I don't know why but it disregards the first row of the table.
Please help:
$row = 0;
$con = mysqli_connect("localhost", "root", "root", "db");
$query = "select * from user_accounts";
$result = mysqli_query($con, $query);
$row = mysqli_fetch_assoc($result);
while($row = $result->fetch_array())
{
echo $row['ID'] . " " . $row['Username'];
echo "<br />";
}
Two things, you're declaring an un-needed variable ($row = 0;)and you're fetching twice, which you shouldn't do:
$con = mysqli_connect("localhost", "root", "root", "db");
$query = "select * from user_accounts";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_assoc($result))
{
echo $row['ID'] . " " . $row['Username'];
echo "<br />";
}
$row = mysqli_fetch_assoc($result);
while($row = $result->fetch_array())
So, you fetch one, ignore it and then continue fetching
Related
I'm writing a forum, and my PHP code shows all the records for the selected column on a simple row.
Code:
<td bgcolor="#FAB1CA">
<?php
$link = mysqli_connect("xxxx", "xxx", "xxxx", "xxxxxx");
if ($link === false) {
die("ERROR: Could not connect. " . mysqli_connect_error());
}
if (isset($_SESSION['logged'])) {
$sql = "Select ID from forum_question ORDER by id DESC";
$result = mysqli_query($link, $sql);
while ($row = mysqli_fetch_assoc($result)) {
$show = $row['ID'];
print_r($show);
}
}
mysqli_close($link);
?>
</td>
<td bgcolor="#FAB1CA">
<?php
$link = mysqli_connect("xxxx", "xxx", "xxxx", "xxxxxx");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
if(isset($_SESSION['logged'])){
$sql= "Select ID from forum_question ORDER by id DESC";
$result = mysqli_query($link, $sql);
while($row = mysqli_fetch_assoc($result)){
$show = $row['ID'];
print_r($show);
echo"<br />";
}
}
mysqli_close($link);
?>
</td>
please echo a "br"-tag. hope your problem will be fixed.
this is my code when i run it
$name=$_GET['name'];
$strSQL = "SELECT * FROM category WHERE name LIKE =" .$name;
$rs = mysql_query($strSQL);
// Loop the recordset $rs
$response = array();
while($row = mysql_fetch_array($rs)) {
$product = array();
$product["id"] = $row["id"];
$product["name"] = $row["name"];
array_push($response, $product);
}
echo json_encode($response);
}
error:mysql_fetch_array() expects parameter 1 to be resource, boolean given in while($row = mysql_fetch_array($rs))
it shows error on this line help me to fix this
you have inserted wrong syntax ....use this
$strSQL = "SELECT * FROM category WHERE name LIKE '%$name'";
You need to open a connection to your database. Something like:
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$name=$_GET['name'];
$strSQL = "SELECT * FROM category WHERE name LIKE '%" .$name . "%'";
// $strSQL = "SELECT * FROM category WHERE name = '" .$name . "'";
$rs = mysql_query($strSQL, $conn);
// Loop the recordset $rs
$response = array();
while($row = mysql_fetch_array($rs)) {
$product = array();
$product["id"] = $row["id"];
$product["name"] = $row["name"];
array_push($response, $product);
}
echo json_encode($response);
?>
Also if you are going to use "LIKE" in your sql, you don't use "=".
It is because You are not sending Query matched with your datatype of the name field.
Try below syntax with ''
$strSQL = "SELECT * FROM category WHERE name LIKE '" .$name . "'";
IF you still getting the same error then try this code
$name=$_GET['name'];
$strSQL = "SELECT * FROM category WHERE name LIKE '" .$name . "'";
$rs = mysql_query($strSQL);
if(mysql_num_rows($rs) > 0){
// Loop the recordset $rs
$response = array();
while($row = mysql_fetch_array($rs)) {
$product = array();
$product["id"] = $row["id"];
$product["name"] = $row["name"];
array_push($response, $product);
}
echo json_encode($response);
}
}
else{ echo json_encode(array('msg'=>"No records found")); }
code:
<?php
session_start();
if ( isset($_GET['user']) && isset($_GET['pass']) )
{
$sql = "SELECT * FROM `users` WHERE `name` = '" . $_GET['user'] . "' AND `password` = '" . $_GET['pass'] . "';";
echo("query: $sql <br />");
$db = mysqli_connect("localhost", "root", "<password here>", "1596");
if (mysqli_connect_errno($db)) { die("err"); }
$result = mysqli_query($db, $sql);
echo($query);
$row = mysqli_fetch_aray($result);
echo($row);
if ($row['name'] == $_GET['user'])
{
$_SESSION['uid'] = $row['name'];
$_SESSION['level'] = $row['level'];
echo("logged in as " . $_SESSION['uid']);
}
}
else
{
die("Error, not enough parameters");
}
?>
If I run that query on server, it is fine.. there is no connect error, so wondering where I went wrong
$db = mysqli_connect("localhost", "root", "<password here>", "1596");
if (mysqli_connect_errno($db)) { die("err"); }
$result = mysqli_query($db, $sql); // line corrected
$row = mysqli_fetch_array($result); // line corrected
I am trying to retrieve information from my database depending on the ID a user types into my URL.
For example: If USER A went to www.exampleurl.com/index.php?id=1 it would echo out the user's information which has an ID of 1. Same thing if the id was 2, 3, etc. Users are entering their information via a form in a different file called submit.php.
Here is my code to retrieve data depending on ID :
<?php
$id = $_GET['id'];
//Variables for connecting to your database.
$hostname = "";
$username = "";
$dbname = "";
$password = "";
$usertable = "";
//Connecting to your database
$con = mysql_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($dbname, $con);
$query = "SELECT * FROM $usertable WHERE id = $id LIMIT 1";
$result = mysql_query($query, $con);
echo "Hello, " . $result['name'];
?>
Any ideas on if my SELECT request is wrong?
EDIT
Here is my code for showing the data altogether in a table. This works fine.
<?php
//Variables for connecting to your database.
$hostname = "";
$username = "";
$dbname = "";
$password = "!";
$usertable = "";
//Connecting to your database
$con = mysql_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($dbname, $con);
//Fetching from your database table.
$query = "SELECT * FROM $usertable";
$result = mysql_query($query, $con);
echo "<table border=1>
<tr>
<th> ID </th>
<th> Name </th>
<th> Age </th>
</tr>";
while($record = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $record['id'] . "</td>";
echo "<td>" . $record['name'] . "</td>";
echo "<td>" . $record['age'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
→ Try This:
You should consider using PHP PDO as it is safer and a more object oriented approach:
$usertable = "";
$database = new PDO( 'mysql:host=localhost;dbname=DB_NAME', 'DB_USER_NAME', 'DB_USER_PASS' );
$statement = $database->prepare('SELECT * FROM $usertable');
$statement->execute();
$count = $statement->rowCount();
if( $count > 0 ) {
$R = $statement->fetchAll( PDO::FETCH_ASSOC );
for( $x = 0; $x < count($R); $x++ ) {
echo "<tr>";
echo "<td>" . $R[ $x ]['id'] . "</td>";
echo "<td>" . $R[ $x ]['name'] . "</td>";
echo "<td>" . $R[ $x ]['age'] . "</td>";
echo "</tr>";
}
}
else { echo "Error!"; }
you need to use mysql_fetch_assoc function for retrieve the results.
$result = mysql_fetch_assoc(mysql_query($query, $con));
echo "Hello, " . $result['name'];
You should be error checking your mysql_querys:
$query = "SELECT * FROM $usertable WHERE id = $id LIMIT 1";
$result = mysql_query($query, $con);
if(!result)
echo mysql_error();
You should also retrieve the results:
$array = mysql_fetch_assoc($result);
I'll consider some secure features like
Check if $_GET['id'] is set and if is int
Apply Mysql escape with mysql_escape_string() function
The table Users contains data but still it shows Records Not Found
<?php
$conn = mysql_connect("localhost", "root", "pass", "Assign1");
$records = mysql_query($conn, "select * from Users");
if(!$records)
{
echo "No Records Found";
exit();
}
while($row = mysql_fetch_array($records))
{
echo $row['name'] . " " . $row['pwd'];
echo "<br />";
}
mysql_close($conn);
?>
You have the parameters to mysql_query reversed. It should be:
$records = mysql_query("select * from Users", $conn);
Your other issue is with the if statement. You're checking if on a query, not on a result set.
Also, I'm sure you probably know but mysql libraries are deprecated and are being removed. You should really learn to use mysqli functions as they will be far more useful to you in the future.
Link to MySQLi documentation - It's really no harder than mysql libraries.
To re-implement in correct libraries:
<?php
$mysqli = new mysqli("localhost", "user", "pass", "database");
$query = $mysqli->query("SELECT * FROM users");
$results = $query->fetch_assoc();
if($results) {
foreach($results as $row) {
echo $row['name'] . " " . $row['pwd'] . "<br/>";
}
} else {
echo "No results found.";
}
?>
Hopefully I didn't just do your whole assignment for you, but it'd probably be worth it to get one more person using mysqli properly.
You have a wrong usage of mysql_query function
use it like this:
<?php
$conn = mysql_connect("localhost", "root", "pass","Assign1");
$result = mysql_query("select * from Users", $conn);
if(!$records)
{
echo "No Records Found";
exit();
}
while($row = mysql_fetch_array($result))
{
echo $row['name'] . " " . $row['pwd'];
echo "<br />";
}
mysql_close($conn);
?>
Lets resolve this issue first.The error it was actually showing is no database selected you have to select the database that needs the code
mysql_select_db("Assign1",$conn);
Hope this code will perfectly sole your issue .Try it once .........
<?php
$conn = mysql_connect("localhost", "root", "pass");
mysql_select_db("Assign1",$conn);
$result = mysql_query("select * from users", $conn);
if(!$result)
{
echo "No Records Found";
exit();
}
while($row = mysql_fetch_array($result))
{
echo $row[0]['name'];
echo "<br />";
}
mysql_close($conn);
?>
here you go
<?php
$conn = mysql_connect("localhost", "root", "pass", "Assign1");
mysql_select_db(' ----your-database-here---', $conn ) ;
$records = mysql_query($conn, "select * from Users");
if(mysql_num_rows($records) > 0 )
{
while($row = mysql_fetch_array($records))
{
echo $row['name'] . " " . $row['pwd'];
echo "<br />";
}
}else
{
echo "No Records Found";
exit();
}
mysql_close($conn);
?>