I found this code once...
$sql_select = "SELECT * FROM users";
$rs_stuff = select($sql_select);
while ($res = mysql_fetch_assoc($rs_stuff)) {
echo ($res("Name")."<br />");
}
It works well, it returns all names found in "Name" col, the problem is I want it to make to return all data in that table, like if I just typed "SELECT * FROM users" on mysql, I don't understand much of PHP, I tried to do this:
echo("<br />
".$res);
But when trying to run this on the page, I just got a empty blank with "Array" written on it... Is it possible to do this without putting the col names in the php?
(Sorry about my English, it is not my main language.)
This happens because $res
is a Array
use:
print_r($res);
or if you want a better view of it:
echo '<pre>';
print_r($res);
echo '</pre>';
OR
you can use:
echo ($res["Name"]."<br />");
also, on this second option, watch out for multidimensional arrays. Your $res
might be one. In which case it will be:
echo ($res[0]["Name"]."<br />");
if you have only one result. I suggest you go with the first choice and see how your $res
looks like before echoing strings ;)
Either way, please find a good PHP tutorial as it is CLEAR that you lack basic knowledge of how to use and manipulate PHP code.
Hope it helps! :D
$sql_select = "SELECT * FROM users";
$rs_stuff = select($sql_select);
echo '<table>';//create the table
while ($res = mysql_fetch_assoc($rs_stuff)) {
echo '<tr><td>'.$res['name'].'</td></tr>';
}
If you learning learn mysqli_
or PDO
their is no point in learning mysql
as they are depriciated.
Try this
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
print_r($row)
}
} else {
echo "0 results";
}
$conn->close();
?>
please put all your rows in an array and then print.
$sql_select = "SELECT * FROM users";
$rs_stuff = select($sql_select);
$arr = array();
while ($res = mysql_fetch_assoc($rs_stuff)) {
$arr[] = $res["Name"];
}
echo '<pre>';
print_r($arr);
echo '<pre>';
Try with this :
foreach($res as $key => $value) {
print $res[$key] . ' => ' . $value;
}
hope that helps :)
It hink this will help:
$sql_select = "SELECT * FROM users";
$rs_stuff = mysql_query($sql_select);
while ($res = mysql_fetch_assoc($rs_stuff)) {
foreach($res as $key => $value) {
echo $key.': '.$value."<br />
";
}
echo "<br />
";
}
Loop trough the array of values in the columns and echo out the cells value in the column. I think this will look like this:
Name: John<br />
Usernmae: johndoe<br />
Age: 36<br />
<br />
Name: Christian<br />
Username: admin<br />
Age: 46<br />
<br />
Don't use echo
to print out an array. When you use echo PHP wants to convert an array to a string. If you only want to get the arrays values use print_r
. Or use this function:
function convertArraytoString($array) {
foreach($array as $key => $value) {
$return .= $key.': '.$value."<br />
";
}
return $return;
}
Than you can convert to the array via echo convertArraytoString($res);