Wednesday, April 25, 2018

php - mysqli_query returns unexpected output



I am a beginner programmer and am having a little trouble getting information from phpmyadmin through mysqli_query. I first connect to the database and then attempt to get the information from the table cbo which is inside the database. I then print out what results from the query which is not the information from the table. instead this is what I get.



mysqli_result Object
(
[current_field] => 0
[field_count] => 8
[lengths] =>

[num_rows] => 12
[type] => 0
)



here is the code I am using. dump just echos the variable.



    $con = mysqli_connect("localhost", "root", "harvard", "cbo projections");
if ( mysqli_connect_errno() ) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();

}
$result = mysqli_query($con,"SELECT * FROM cbo");
dump( $result );
?>


thanks for the help.


Answer



$result is just a resource that contains the resultset. You have to fetch data from it. Read mysqli_fetch_assoc or mysqli_fetch_array




EXAMPLE:



if ($result = mysqli_query($link, $query)) {
while ($row = mysqli_fetch_assoc($result)) {
//Display fields here according to your table structure
}
mysqli_free_result($result);
}



EDIT



Yes, why not : You could do something like



    while ($row = mysqli_fetch_assoc($result)) {
$records[]=$row;
}


This will create an array named records which will contain all your fetched rows and then you can later access that array and process accordingly



No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...