Simple SQL & PHP query failing - php

I haven't used PHP or SQL in a while, and can't seem to figure out why this query is failing. Probsbly going to be something silly :).
<php?
$dbconn = mysql_connect("localhost","xxx","xxx");
if (!$dbconn)
{
die('Error connecting to DB!');
}
if (! #mysql_select_db('rdrkictj_rsvp') )
{
die(mysql_error());
}
if(isset($_GET['id'])){
$ID = $_GET['id'];
$stockcount = $_GET['stockcount'] - 1;
}
else
die(mysql_error());
mysqli_query($dbconn,'UPDATE products SET stockcount = "5" WHERE id = "1"');
mysqli_close($dbconn);
?>
I receive the following errors:
Warning: mysqli_query() expects parameter 1 to be mysqli, resource
given in /home/rdrkictj/public_html/test/buyit.php on line 19
Warning: mysqli_close() expects parameter 1 to be mysqli, resource
given in /home/rdrkictj/public_html/test/buyit.php on line 21
Any advice would be greatly appreciated.

<php? should be <?php, also you're mixing mysql functions with mysqli functions. Choose one of them (mysqli). So, change: he
mysql_connect("localhost","xxx","xxx");
to the mysqli equivalent:
mysqli_connect("localhost","xxx","xxx");
Also change mysql_error() to mysqli_error(),
and finally change:
#mysql_select_db
to:
#mysqli_select_db

OK so that is what happens when you mix two tutorial together -_-
Thanks to both respondents. The following code works:
$con=mysqli_connect("localhost","xxx","xxx","xxx");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,'UPDATE products SET stockcount = "5" WHERE id = "1"');
mysqli_close($con);

Use either mysqli or mysql (mysqli is recommended)
For example:
$dbconn = mysql_connect("localhost","xxx","xxx");
should be
$dbconn = mysqli_connect("localhost","xxx","xxx");
Same for others as well
Full Code:
<?php
$dbconn = mysqli_connect("localhost","xxx","xxx") or die('Error connecting to server');
if (! #mysqli_select_db($dbconn, 'rdrkictj_rsvp') )
{
die(mysqli_error($dbconn));
}
if(isset($_GET['id'])){
$ID = $_GET['id'];
$stockcount = $_GET['stockcount'] - 1;
}
else
die(mysqli_error($dbconn));
mysqli_query($dbconn,'UPDATE products SET stockcount = "5" WHERE id = "1"');
mysqli_close($dbconn);
?>

Related

Outputting contents of database mysqli

Hi I know this is a little general but its something I cant seem to work out by reading online.
Im trying to connnect to a database using php / mysqli using a wamp server and a database which is local host on php admin.
No matter what I try i keep getting the error Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given when i try to output the contents of the database.
the code im using is:
if (isset($_POST["submit"]))
{
$con = mysqli_connect("localhost");
if ($con == true)
{
echo "Database connection established";
}
else
{
die("Unable to connect to database");
}
$result = mysqli_query($con,"SELECT *");
while($row = mysqli_fetch_array($result))
{
echo $row['login'];
}
}
I will be good if you have a look at the standard mysqli_connect here
I will dont seem to see where you have selected any data base before attempting to dump it contents.
<?php
//set up basic connection :
$con = mysqli_connect("host","user","passw","db") or die("Error " . mysqli_error($con));
?>
Following this basic standard will also help you know where prob is.
you have to select from table . or mysqli dont know what table are you selecting from.
change this
$result = mysqli_query($con,"SELECT *");
to
$result = mysqli_query($con,"SELECT * FROM table_name ");
table_name is the name of your table
and your connection is tottally wrong.
use this
$con = mysqli_connect("hostname","username","password","database_name");
you have to learn here how to connect and use mysqli

SQL/PHP scripting issue

I wrote this line of code, but i do not know what happened. I have looked all around the internet for the solution, but none of them seem to fix my issue. I get:
Warning: mysql_query() expects parameter 1 to be string, resource given in /home/mylittle/public_html/style1.php on line 12
yes
When i enter the page. It does not update the style thing in my database. Please help me. I am desperate!
$dbewds = mysql_connect("localhost","mylittle_pony","lol123", "mylittle_pony") or die("Couldn't connect!");
if ($_SESSION['username']) {
$unw = $_SESSION['username'];
$style = 1;
mysql_query($dbewds,"UPDATE `users` SET `style` = '".$style."' WHERE `username` = '".$unw."'");
echo "yes";
} else {
echo "no";
}
?>
$dbewds = mysql_connect("localhost","mylittle_pony","lol123") or die("Couldn't connect!");
mysql_select_db("mylittle_pony");
if (isset($_SESSION['username'])) {
$unw = $_SESSION['username'];
$style = 1;
$query=mysql_query("UPDATE `users` SET `style` = '".$style."' WHERE `username` = '".$unw."'",$dbewds);
if(!$query){
die("query failed".mysql_error());
}
echo "yes";
} else {
echo "no";
}
the connection should be the second variable
I would advise the steps you debug the likely problems next time:
1. try to understand the warning/error message
for example: "mysql_query() expects parameter 1 to be string, resource give", so figure what is a string, what is a resource, according to your code
2. read the manual
go to http://us2.php.net/manual/en/ and search "mysql_query", you can get http://us2.php.net/manual/en/function.mysql-query.php, so figure out how to use the function,
pay attention to the parameters and return, and run the examples under the function intro
3. check your code
btw, mysql_query() will be deprecated as of PHP 5.5.0, MySQLi or PDO_MySQL is better.

Insert Into doesn't work

I want to insert data fields like: id=1, id_page=12356. I read the id_page out of an html document and am always getting this error message:
Warning: mysql_query() expects parameter 1 to be string, resource
given in
/mnt/webi/b0/44/53443744/htdocs/digitalpiano-test/kitareader.php on
line 14 error
<?php
$con = mysql_connect("rdbms.strato.de","U1363575","asdasd123","DB1363575");
if (mysqli_connect_errno())echo "Failed to connect to MySQL: " . mysqli_connect_error();
$name = file_get_contents('kitas.html'); $array_name = explode("<tr>", $name);
foreach($array_name as $value) {
$value2 = explode('<a href="kitaDetails.aspx?ID=',$value);
$value3 = explode('">',$value2[1]);
$id_page = $value3[0];
$eintragen = mysql_query($con,"INSERT INTO kita_berlin (id_page) VALUES ('$id_page')") or die ("error");
} ?>
What is wrong?
MySQL takes the query as the first parameter and the connection as the second parameter.
$eintragen = mysql_query("INSERT INTO kita_berlin (id_page) VALUES ('$id_page')", $con) or die ("error");
This would work, but you really should look at using MySQLi or PDO as MySQL is deprecated now.
Moreover, there's a much cleaner way to get the id_page attribute, by directly using GET, instead of exploding the id_page out.

update mysql_num_rows to new Mysqli standard: mysqli_stmt_num_rows

I have a PHP page with some Mysqli that I am attempting to convert from MySql. I think I've converted most of it correctly, but I am getting the following error message when I try to execute the code (below):
Connection was OK!
Warning: mysqli_stmt_num_rows() expects parameter 1 to be mysqli_stmt, object given in /quantityremaining5.php on line 25
9999
I'm a bit new to this, so please be gentle - what is that I'm doing wrong? thanks!
<?php
include 'quantitytest_config.php';
// Create connection to MySQL
$link = mysqli_connect($hostname, $username, $password);
// Check connection
if (mysqli_connect_errno($link))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else { echo "Connection was OK!\n";}
//select a database to work with
$selected = mysqli_select_db($link, "grace59_countdown");
// or die("Could not select countdown");
// use if TWO SEPARATE columns for date and time
//execute the SQL query and return records
$result = mysqli_query($link,
"SELECT items
FROM cases
WHERE datetime<=NOW()
Limit 1 ");
if(mysqli_stmt_num_rows($result) == 0){
echo "9999";
} else {
//fetch tha data from the database
while ($row = mysqli_fetch_array($result)) {
echo "Quantity:".$row{'items'}."<br>";
}
}
//close the connection
mysqli_close($link);
?>
Use mysqli_num_rows($result) or $result->num_rows. As the name indicates, mysqli_stmt_num_rows() is intended to be used with a mysqli_stmt object (as returned by mysqli_prepare()).
See the documentation.

Cannot Display Data from MySQL table

I've got a pretty standard call to a MySQL database and for some reason I can't get the code to work. Here's what I have:
$mysqli = mysqli_connect("localhost","username","password");
if (!$mysqli)
{
die('Could not connect: ' . mysqli_error($mysqli));
}
session_start();
$sql = "SELECT * FROM jobs ORDER BY id DESC";
$result = $mysqli->query($sql);
$num_rows = mysqli_num_rows($result);
Now, first, I know that it is connecting properly because I'm not getting the die method plus I added an else conditional in there previously and it checked out. Then the page displays but I get the errors:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in blablabla/index.php on line 11
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in blablabla/index.php on line 12
I've double-checked my database and there is a table called jobs with a row of "id" (it's the primary row). The thing that confuses me is this is code that I literally copied and pasted from another site I built and for some reason the code doesn't work on this one (I obviously copy and pasted it and then just changed the table name and rows accordingly).
I saw the error and tried:
$num_rows = $mysqli_result->num_rows;
$row_array = $mysqli_result->fetch_array;
and that fixed the errors but resulted in no data being passed (because obviously $mysqli_result has no value). I don't know why the error is calling for that (is it a difference in version of MySQL or PHP from the other site)?
Can someone help me track down the problem? Thanks so much. Sorry if it's something super simple that I'm overlooking, I've been at it for a while.
You didn't selected the database
$mysqli = mysqli_connect("localhost","username","password","database");
The problem is you haven't selected the database.
use this code for select database.
$mysqli = mysqli_connect("localhost","username","password");
mysqli_select_db("db_name",$mysqli);
You have to select database in order to fire mysql queries otherwise it will give you error.
I believe that schtever is correct, I do not think you are selecting the database. It isn't in the code snip and if you search online you see other people with similar errors and it was because the database wasn't selected. Please let us know if you selected a database before anything else is checked. Thanks.
Try this:
session_start();
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
if ($mysqli->connect_errno)
{
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
$mysqli->close();
}
$query ="SELECT * FROM jobs ORDER BY id DESC";
$values = $mysqli->query($query);
if($values->num_rows != 0)
{
while($row = $values->fetch_assoc())
{
//your results echo here
}
}
else
{
//if no results say so here
}
See this manual for mysqli_connect you can select the database right in this function.

Categories