Wednesday, 25 April 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

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...