I connect to database and all is correct, I do the select and all is correct. But the problem comes when I tried to obtain all the rows. I tried with all mysql_fetch... (array, assoc...)
$registros = mysql_query("SELECT * FROM 22bf654e4dfa688d0ec6add2f6f7bf76", $con);
$reg = mysql_fetch_array($registros);
if( ($reg)
{
echo $reg;
}
Keep in mind mysql_
library is deprecated and you should be using PDO or MySQLi.
Using PDO your code would look like this:
<?php
// Your database info
$db_host = '';
$db_user = '';
$db_pass = '';
$db_name = '';
$con = new PDO("mysql:host={$db_host};dbname={$db_name}", $db_user, $db_pass);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM 22bf654e4dfa688d0ec6add2f6f7bf76";
$result = $con->prepare($sql);
$result->execute();
$reg = $result->fetchALL(PDO::FETCH_ASSOC);
$con = NULL;
print_r($reg);
// To show just one row you can use:
print_r($reg[0]);
// So let's say you have a field called "name", then u could use:
echo $reg[0]['name'];
You need to iterate over the results, this will store all the data into an array $reg
:
<?php
// your database info here
$db_host = '';
$db_user = '';
$db_pass = '';
$db_name = '';
$conn = mysql_connect($db_host, $db_user, $db_pass);
if (!$conn)
{
die("Unable to connect to DB: " . mysql_error());
}
if (!mysql_select_db($db_name))
{
die("Unable to select {$db_name}: " . mysql_error());
}
$sql = "SELECT * FROM 22bf654e4dfa688d0ec6add2f6f7bf76";
$registros = mysql_query($sql);
if (!$registros)
{
die("Error: " . mysql_error());
}
$reg = array();
while ($row = mysql_fetch_assoc($registros))
{
$reg[] = $row;
}
print_r($reg);
// To show just one row you can use:
print_r($reg[0]);
// So let's say you have a field called "name", then u could use:
echo $reg[0]['name'];
You should be using mysqli
instead of mysql
, but that's a whole other answer.
while($row = mysql_fetch_array($result)) {
echo $row['column-name-here'];
}
That will loop through ALL results from your query. If you only have 1 result, it will only show 1. If you only WANT 1 result, then specify in your query to which you want like
SELECT * FROM table WHERE something = something;
$reg is an array of all rows; you want to use foreach to iterate through it and then print_r instead of echo on each iteration because that will be an array of columns for that row
If you only want the first row, print_r($reg[0])
But better yet use a where clause in your query or a limit statement for better performance. Don't return more data than you need.