PHP and MySql hitcounter problem? - php

Okay I'm new to php and mysql I tried to build a web page hit counter but my page counter dosen't update correctly and it gets the following warnings listed below.
Warning: mysqli_query() expects at least 2 parameters, 1 given in
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in
Here is the php code below.
<?php
$page = $_SERVER['SCRIPT_FILENAME'];
// Query member data from the database and ready it for display
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT id page FROM mysql_counter_logs WHERE page = '$page'");
if (mysqli_num_rows($dbc) == 0) {
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"INSERT INTO mysql_counter_logs (page) VALUES ('$page')");
mysqli_query($dbc);
} elseif (mysqli_num_rows($dbc) == 1) {
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"UPDATE mysql_counter_logs SET hits = hits + 1 WHERE page = '$page'");
mysqli_query($dbc);
}
//Retreives the current count
$count = mysqli_fetch_row(mysqli_query($mysqli,"SELECT hits FROM mysql_counter_logs"));
if (!$dbc) {
// There was an error...do something about it here...
print mysqli_error();
}
//Displays the count on your site
print "$count[0]";
?>

An extract of your code :
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"INSERT INTO mysql_counter_logs (page) VALUES ('$page')");
mysqli_query($dbc);
First of all, according to the manual of mysqli_query, when used as a function :
mixed mysqli_query ( mysqli $link , string $query [, int $resultmode ] )
Which means you have to pass your $mysqli variable as first parameter, and your query as a second parameter.
What I don't get is why you are using mysqli_query this way on the third line I copy-pasted, while you are using it correctly in the other parts of your code ?
Are you sure you mean to use that function, here ?
Also, knowing on which lines you are getting those error messages might help ;-)
Check you don't do anything else "wrong" there -- like " mysqli_query() expects at least 2 parameters, 1 given "
Thinking about it : mysqli_query will return boolean false if there is an error.
In that case, you should not call mysqli_num_rows on the return value of mysqli_query, as it's not a valid mysqli_result -- hence the second error message.
You are checking for $dbc at the end of your script, but it might be interesting to check it too a biit sooner -- actually, after each call to mysql_query, I'd say.
Also, as a sidenote : you probably don't need to instance the mysqli class that many times : just instanciate it once, and then use the $mysqli variable for there rest of your script.
Just for security, and to avoid any trouble, you might also want to use mysqli_real_escape_string on the $page variable, before injecting it into the queries.
Oh, and, btw : you might be interested by the INSERT ... ON DUPLICATE KEY UPDATE Syntax that MySQL allows to use ;-)
(Well, that is if page is the primary key of your table, at least...)

Related

Getting error when call mysql stored procedure from php

I have triggered the stored procedure from php. I have passed the input parameters also as shown.
$id = 1;
$nameDetail = 'raj';
$result = mysqli_query('CALL InsertDetails($id,$nameDetail)');
But getting below error.
mysqli_query() expects at least 2 parameters, 1 given ...
Please suggest a solution.
The issue is that you're not set the mysqli connection.please try this
$connection = mysqli_connect('localhost','username','password','db');
$result = mysqli_query($connection,'CALL InsertDetails($id,$nameDetail)');
You need to pass in the connection as the first parameter:
// connect to DB
$connect = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
// run procedure
$result = mysqli_query($connect, 'CALL InsertDetails($id,$nameDetail)');

Warning: mysqli_fetch_object() expects parameter 1 to be mysqli_result, boolean given in [duplicate]

This question already has answers here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(2 answers)
Closed 3 years ago.
Warning: mysqli_fetch_object() expects parameter 1 to be mysqli_result, boolean given in /public_html/header.php on line 35
In the header.php,
Connect to database:
$connect=mysqli_connect("localhost","$db_name","$password")
$sql_logo="SELECT * FROM `logo`";
$query_logo=mysqli_query($connect,$sql_logo);
On line 35:
<?php
while($result=mysqli_fetch_object($query_logo)){
?>
<img src="res/img/<?php echo $result->logo_img?>">
<?php } ?>
in my database:
the table name is logo and the column name is logo_img.
I don't know what happens, because when I try in XAMPP it's work using that's code.
This $connect=mysqli_connect("localhost","$db_name","$password")
2 things here.
The $db_name, that must be the last parameter and you've a missing closing semi-colon.
The syntax is:
host
username
password (if any)
database
As per the manual
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
http://php.net/manual/en/function.mysqli-connect.php
Make sure you also did properly assign those variables.
If there's no password, you still need the parameter for it, but as empty.
I.e.:
$link = mysqli_connect("127.0.0.1", "my_user", "", "my_db");
^ that is for the password parameter
Check for errors also:
http://php.net/manual/en/mysqli.error.php
Your $query_logo is probably FALSE instead of the results you're trying to fetch. This could mean either your mysqli_connectfails, or the $sql_logo query fails.
Print the errors out using echo mysqli_error($connect) and you will find your answer there.
Also, I'd recommend having a look at object oriented mysqli, looks like you're still figuring stuff out, don't get used to procedural or you'll regret it later.
Please refer the PHP-Manual
http://php.net/manual/en/function.mysqli-connect.php
for getting the better idea of mysqli syntax.
And for displaying records try to use syntax and code given below:
$conn = mysqli_connect("127.0.0.1", "db_username", "db_password", "Database_anme");
$limit = 10; //$_POST['limit'];
$start = 0; //$_POST['start'];
$r = mysqli_query($conn, "SELECT * FROM `TABLE_NAME` DESC LIMIT $start,$limit");
while($quotos = mysqli_fetch_object($r)){
// Your code goes here
}

mysqli_error() expects parameter 1 to be mysqli, null given

I have a a form that pulls data from a database(mysql to be specific) and echos the data into the value section of <input> tags. It doesn't seem to be working I have coded a view section of my website to do the same thing but from a different table in my database. I use the same code to make making changes easy and if another developer works on my site in the future. Anyway it doesn't seem to be working I'm not sure why though.
The full error I get:
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/caseol5/public_html/jj/admin/news_update.php on line 9
Here is line 9 that the error is referring to:
$result = mysqli_query($link,$sql);
I know that both of those function are not null as I did:
echo $link
echo $sql
before that line after I started feting the error and they both are not null.
Here is the full code segment:
$nid = $_GET['nid'];
include ("../sql/dbConnect.php");
$sql = "SELECT * FROM jj_news WHERE news_id = $nid";
echo "<p>The SQL Command: $sql </p>";
echo "<p>Link: $link </p>";
$result = mysqli_query($link,$sql);
if (!$result)
{
echo "<h1>You have encountered a problem with the update.</h1>";
die( "<h2>" . mysqli_error($link) . "</h2>") ;
}
$row = mysqli_fetch_array($result);
$ntitle = $row['news_title'];
$ntline = $row['news_titleline'];
$ndesc = $row['news_desc'];
$nother = $row['news_other'];
I have looked into mysqli_query and I can't find anything I'm missing. I have also tired breaking the code down (and running parts of it and it gives the same error. My guess is it something small that I missed. I've looked at other question on this site that do that are a little similar but none seem to help. I've been looking at this for a while now and need another pair of eyes.
Update
As requested the contents of my dbconnect.php file:
$hostname = "localhost";
$username = "caseol5_jjoes";
$database = "caseol5_jj_site";
$password = "password1";
$link = mysqli_connect($hostname, $username, $password, $database);
$link = mysqli_connect($hostname,$username,$password,$database) or die("Error " . mysqli_error($link));
if (!$link)
{
echo "We have a problem!";
}
As clearly stated in the error message, mysqli_querydocs expects the first parameter to be a mysqli resource. In your case, this parameter is called $link but it holds a null value. A proper mysqli resource is normally obtained from connecting with the database by making use of mysqli_connectdocs
I expect the ../sql/dbConnect.php file holds the logic to connect with the database. Verify whether the $link variable is indeed initialized there. If it's not there, try to find an occurrence of mysqli_connect - maybe the resource is set to a different variable.
Without knowing what exactly is in ../sql/dbConnect.php, your problem right now is that you do not have a valid mysqli resource to use for mysqli_query.

multiple mysql connections on one page php

I am getting errors (not really errors, just not working properly) and I'm trying to figure out if it's related to my sql connections. I have 2 separate connections to two entirely different databases.
$dshost = "kjbkb";
$dsdatabase = "kjhk";
$dsusername = "hjgfytdf";
$dspassword = "jhv";
mysql_connect($dshost,$dsusername,$dspassword);
mysql_select_db($dsdatabase) or die( "Unable to select database");
$sql = "SELECT * FROM users WHERE `username` = '".$_POST['paydl']."'";
$cheeseburger = mysql_query($sql);
$res = mysql_fetch_array($cheeseburger);
$autobus_user = $res['id'];
mysql_close(); //close first connection
$db_host = "khgv";
$db_user = "trdstx";
$db_password = "txz";
$db_database = "gfxx";
mysql_connect($db_host, $db_user, $db_password) or die("Unable to connect to host");
mysql_select_db($db_database) or die( "Unable to select database");
$ASDFASDF = "SELECT * FROM `autobus` WHERE `user_id` = '".$autobus_user."' LIMIT 3";
$BobDoleDontNeedThis = mysql_query($ASDFASDF);
$resnumbatwo = mysql_fetch_array($BobDoleDontNeedThis);
mysql_close(); //close 2nd connection
Am I doing this right? Why is $resnumbatwo returning false?
When using multiple MySQL connections in PHP, you have to supply a fourth argument telling PHP to actually create new connections like this (this is very important, if you are using two connections to the same host):
$db1 = mysql_connect($host1, $user1, $passwd1, true);
$db2 = mysql_connect($host2, $user2, $passwd2, true);
If the fourth argument is not used, and the parameters are the same, then PHP will return the same link and no new connection will be made.
After this you should use "mysql_query" with an extra parameter than defines which connection to use:
$res1 = mysql_query($sql1, $db1) or die(mysql_error($res1));
$res2 = mysql_query($sql2, $db2) or die(mysql_error($res2));
The description of the fourth parameter from php.net is:
"If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters. In SQL safe mode, this parameter is ignored."
See more at:
http://www.php.net/manual/en/function.mysql-connect.php
mysql_fetch_array will return false if there are no matching records returned from the query. Please see the following entry from the API reference docs:
Return Values:
Returns an array of strings that corresponds to the fetched row, or
FALSE if there are no more rows. The type of returned array
depends on how result_type is defined. By using MYSQL_BOTH
(default), you'll get an array with both associative and number
indices. Using MYSQL_ASSOC, you only get associative indices (as
mysql_fetch_assoc() works), using MYSQL_NUM, you only get number
indices (as mysql_fetch_row() works).
Better save each connection handler in a variable and transmit it to correspondent mysql_query().
Example:
$dbch1 = mysql_connect($dshost, $dsusername, $dspassword);
$dbch2 = mysql_connect($db_host, $db_user, $db_password);
$cheeseburger = mysql_query($sql, $dbch1);
$BobDoleDontNeedThis = mysql_query($ASDFASDF, $dbch2);
You can use
$dbch1 = mysql_connect($dshost, $dsusername, $dspassword,true);
$dbch2 = mysql_connect($db_host, $db_user, $db_password,true);
$cheeseburger = mysql_query($sql, $dbch1);
$BobDoleDontNeedThis = mysql_query($ASDFASDF, $dbch2);
Using php7
you can use the mysqli_change_user() function.
Like in the examples at the php.net page: http://php.net/manual/en/mysqli.change-user.php

mysql_query() expects parameter 2 to be resource, boolean given in [duplicate]

This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
(31 answers)
Closed 8 years ago.
I looked at the other answers to problems similar to mine but cant seem to solve this.
Here is the code.
$connection = mysql_connect("localhost","root","starwars");
$conn = mysql_select_db("project", $connection);
// This code assumes $itemID is set to that of
// the item that was just rated.
// Get all of the user's rating pairs
$sql = "SELECT DISTINCT r.itemID, r2.ratingValue - r.ratingValue
as rating_difference
FROM rating r, rating r2
WHERE r.userID=$userID AND
r2.itemID=$itemID AND
r2.userID=$userID;";
$db_result = mysql_query($sql, $conn);
echo "The result is {$db_result}";
$num_rows = mysql_num_rows($db_result)or die('Cannot Execute:'. mysql_error());
The error being displayed is:
Warning: mysql_query() expects parameter 2 to be resource, boolean
given in C:\xampp\htdocs\recomender\ratingfiles\class.rating.php on
line 177
Line 177 is
$db_result = mysql_query($sql, $conn);
And if I echo $conn it gives the value of "1" which I thought was equal to true, thus boolean, any ideas?
Pass $connection as the second parameter, not $conn.
You assign the result of mysql_select_db to $conn, and mysql_select_db returns true or false, not a connection resource.
This probably means $conn is false, meaning it didnt get setup correctly. You may want to check how you have set that up and ensure the database connection details are correct and the server this is running on can access the database server.
Take a look at the return values on this page:
http://php.net/manual/en/function.mysql-connect.php
change first 2 lines to
$conn = mysql_connect("localhost","root","starwars");
mysql_select_db("project", $conn);
or, better, just refrain from using connection variable at all
mysql_connect("localhost","root","starwars");
mysql_select_db("project");
...
$db_result = mysql_query($sql);
$db_result = mysql_query($sql, $connection);
Second argument to mysql_connect must have
The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an E_WARNING level error is generated.
http://php.net/manual/en/function.mysql-query.php

Categories