mysql_fetch_array to pull values into a variable - php

I know that this is dozy, but getting to the end of my tether, this should work, but I can't see why I can't place the first value of my order table into the variable order_id, can anyone see my mistake?
Thanks
$con = mysql_connect("localhost", "tim", "password");
if (!$con) {
die('Could not connect: '.mysql_error());
}
$sql = "SELECT TOP 1 order_order_id FROM tbl_order ORDER BY order_id DESC";
$result = mysql_query($sql, $con);
while ($rows = mysql_fetch_array($result)) {
$order_id = $rows;
}

Use this code and let me know if it works:
$con = mysql_connect("localhost","tim","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT TOP 1 order_order_id FROM tbl_order ORDER BY order_id DESC";
$result = mysql_query($sql,$con);
if (mysql_num_rows($result)) {
$rows = mysql_fetch_assoc($result);
$order_id = $rows['order_order_id'];
} else {
die('No order ID found');
}

Related

PHP: Warning: mysql_fetch_array() expects parameter 1 to be resource

I got this error while adding this code. Would appreciate some help. It's for a CS jackpot site.
$sitename = "website.com"; // YOUR DOMAIN
$link = mysql_connect("localhost", "db_user", "db_pass"); // MYSQL , LOCALHOOST , USERNAME , PASSWORD
$db_selected = mysql_select_db('db_name', $link); // MYSQL DATABASE
mysql_query("SET NAMES utf8");
function fetchinfo($rowname,$tablename,$finder,$findervalue) {
if($finder == "1") $result = mysql_query("SELECT $rowname FROM $tablename");
else $result = mysql_query("SELECT $rowname FROM $tablename WHERE `$finder`='$findervalue'");
while($row = mysql_fetch_assoc($query))
return $row[$rowname];
}
Some tips:
Use mysqli better than mysql
Split the vars in the query, like "SELECT ".$rowname." FROM ".$tablename;
Hope this help...
<?php
//Open a new connection to the MySQL server
$mysqli = new mysqli('host','username','password','database_name');
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
//MySqli Select Query
$result = $mysqli->query("SELECT id, product_name FROM products");
while($row = $results->fetch_assoc()) {
echo $row["id"].' - '.$row["product_name"].'<br>';
}
$results->free();
$mysqli->close();

No database selected on PHP

I got this code to create a JSON file with the data I have in my database:
<?php
$connect = mysql_connect("localhost", "root", "123", "boxr") or die("Error connecting with the host.");
$sql = mysql_query("SELECT * FROM users") or die(mysql_error());
$response = array();
$users = array();
$result = mysql_query ($sql);
while ($row = mysql_fetch_array($result)) {
$gamertag = $row['gamertag'];
$banned = $row['banned'];
$users[] = array('gamertag' => $gamertag, 'banned' => $banned);
}
$response['users'] = $users;
$fp = fopen('users.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);
?>
The problem is when I run the script it shows the following:
No database selected
I checked my code twice but I can't figure out why its telling me that.
You have to select the database name with:
mysql_select_db ('your_db_name');
Well you can do it like that:
$connect = mysql_connect("localhost", "root", "123") or die("Error connecting with the host.");
mysql_select_db("boxr", $connect);
And later, correct it:
$sql = mysql_query("SELECT * FROM users") or die(mysql_error());
while ($row = mysql_fetch_array($result))...
By:
$sql = mysql_query("SELECT * FROM users") or die(mysql_error());
while ($row = mysql_fetch_array($sql))...
If you are calling the function with the name $sql, you can't later put the name of $result.
mysql_* functions are deprecated but you could change your query
$sql = mysql_query("SELECT * FROM dbname.users") or die(mysql_error());
Use :
$db_selected = mysql_select_db('db_name', $connect);
if (!$db_selected) {
die ('Can\'t use db_name : ' . mysql_error());
}

MySQL won't update with URL variable

I'm having troubles getting my code to work properly. If I type it into phpMyAdmin it works, but when I try it in the code, it doesn't update the database.
<?php
$con = mysql_connect("SERVER","USER","PASSWORD");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("DATABASE", $con);
$sp=mysql_real_escape_string($_GET['file']);
$query = "UPDATE TDB SET WEIGHT=100000 WHERE PATH='$sp'";
mysql_close($con);
?>
Try out this code snippet and see how you get on.
<?php
$con = mysql_connect("SERVER","USER","PASSWORD");
if (!$con) {
die('Could not connect: ' . mysql_error());
} else {
mysql_select_db("DATABASE", $con);
$sp=mysql_real_escape_string($_GET['file']);
$query = "UPDATE TDB SET WEIGHT=100000 WHERE PATH='$sp'";
$result = mysql_query($query);
mysql_close($con);
}
?>
I would recommend doing it this way as mysql is no longer supported by PHP.
<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
if (!$mysqli) {
die('Could not connect: ' . $mysqli->connect_error);
} else {
$sp = $mysqli->real_escape_string($_GET['file']);
$query = "UPDATE TDB SET WEIGHT=100000 WHERE PATH='$sp'";
$mysqli->query(query);
$mysqli->close();
}
?>
You're not EXECUTING your query. You're just defining a string that happens to contain some SQL, e.g.
$sql = "blah blah blah";
$result = mysql_query($sql) or die(mysql_error()); <--forgot this
<?php
$con = mysql_connect("SERVER","USER","PASSWORD");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("DATABASE", $con);
$sp=mysql_real_escape_string($_GET['file']);
$sql = "UPDATE TRACKDB SET WEIGHT=100000 WHERE PATH='$sp'";
$result = mysql_query($sql) or die(mysql_error());
mysql_close($con);
?>

MySQL Query and display not working?

My MySQL database is setup with the name "chatterr" with the table name, "maillist". I am currently using the php below:
<?php
mysql_connect("localhost","root","");
//query databae
$query = "SELECT * FROM maillist ORDER BY id DESC LIMIT 4";
$result=mysql_query($query) or die('Error, insert query failed');
$row=0;
$numrows=mysql_num_rows($result);
while($row<$numrows) {
$id=mysql_result($result,$row,"id");
$first_name=mysql_result($result, $row, "first_name");
$last_name=mysql_result($result, $row, "last_name");
?>
<?php echo $id; ?>
<?php
$row++;
}
?>
It works on localhost but doesn't work in PHP. What's wrong with the code?
Select a database with mysql_select_db before querying it
mysql_connect("localhost","root","");
mysql_select_db("chatterr");
or specify the database name in the query
$query = "SELECT * FROM chatterr.students ORDER BY id DESC LIMIT 4";
UPDATE: Besides that your is connection probably failing. Change
mysql_connect("localhost","root","");
to
$db = mysql_connect("localhost","root","");
if (!$db) {
die('Could not connect: ' . mysql_error());
}
to see if that's the case.
UPDATE3: Put it all together. Although that code has a LOT room for improvement it works perfectly fine on my machine.
<?php
$db = mysql_connect("localhost","root","");
if (!$db) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("chatterr", $db);
if (!$db_selected) {
die ('Could not select db: ' . mysql_error());
}
//query databae
$query = "SELECT * FROM test.students ORDER BY id DESC LIMIT 4";
$result=mysql_query($query) or die('Error, insert query failed');
$row=0;
$numrows=mysql_num_rows($result);
while($row<$numrows)
{
$id=mysql_result($result,$row,"id");
$first_name=mysql_result($result,$row,"first_name");
$last_name=mysql_result($result,$row,"last_name"); ?>
<?php echo $id; ?>
<?php
$row++;
}
?>
And please, don't use mysql_* functions for new code. They are
deprecated. Use prepared
statements with either PDO
or MySQLi.
Just do it this way :
<?php
$con = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("chatterr",$con) or die(mysql_error());
//query database
$query = "SELECT * FROM maillist ORDER BY id DESC LIMIT 4";
$result=mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$id = $row["id"];
$first_name = $row["first_name"];
$last_name = $row["last_name"];
}
?>

PHP not finding mySQL database?

I must be doing something wrong
I have a very simple script and a very simple database
No idea why it's not working..
Please help
<?php
error_reporting(E_ALL);
$link = mysql_connect('localhost', 'root', 'password');
if(!$link)
{
die('Could not connect: ' . mysql_error());
}
$database = mysql_select_db('test_db', $link);
if(!$database)
{
die('Could not connect to database: ' . mysql_error());
}
$result = mysqli_query($link, "SELECT forename FROM users WHERE id='1'");
if(!$result)
echo 'PROBLEM';
$row = mysqli_fetch_array($result);
echo $row[0];
?>
It's not even giving any errors, just echoing 'Problem'...
The database connects fine, and there is 1 user in the database with an ID of 1 and forename is Cristian.
In $result = mysqli_query try just useing $result = mysql_query same in $row = mysqli_fetch_array should help
Try replaceing you code from $result onward with this
$result = mysql_query("SELECT forename FROM users WHERE id='1'");
if(!$result)
echo 'PROBLEM';
$row = mysql_fetch_array($result);
echo $row[0];
?>

Categories