I get this error, I tried going through other similar threads but it didn't help.
here is php
$host = "http://www.example.net";
$hostname = "localhost";
$username = "aaa";
$password = "sss";
$userstable = "ddd";
$dbName = "fff";
if ($url != $host){
$con = mysqli_connect($hostname, $username, $password, $dbName);
//#mysqli_select_db("$dbName");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = mysqli_fetch_row(mysqli_query($con, "SELECT * FROM $userstable where(url = '$url')"));
if ($query == false){
$hits = "1";
$query2 = "INSERT INTO $userstable (url,hits) VALUES('$url','$hits')";
}
else {
$hitquery = "SELECT `hits` FROM $userstable where url = '$url'";
$result = mysqli_query($con, $hitquery);
$hits = mysqli_fetch_assoc($result, 0, 'hits');
//$hits = mysqli_result(mysqli_query("SELECT `hits` FROM $userstable where url = '$url'"), 0, "hits");
$query2 = "UPDATE $userstable SET `hits` = hits+1 where url = '$url'";
}
mysqli_query($con, $query2);
}
if(!$url) {
$url = "$host";
}
eror generate on line 157
$hits = mysqli_fetch_assoc($result, 0, 'hits');
How to fix this mysqli?
Syntax mysqli_fetch_assoc is:
array mysqli_fetch_assoc ( mysqli_result $result )
Try (for PHP >=5.4):
$hits = mysqli_fetch_assoc($result)['hits'];
From the codes, I see that you want to UPDATE the final records from hits.
$hitquery = "SELECT * FROM $userstable where url = '$url'";
$result = mysqli_query($con, $hitquery);
while(null !== ($hits= mysqli_fetch_assoc($result))) {
$query2 = "UPDATE $userstable SET `hits` = hits+1 where url = '$url'";
}
Related
I have a problem
I want to echo the value of "points" column.
What I tried, but did not work:
$stmt = $mysqli->prepare("SELECT points FROM member_profile WHERE user_id = '$firstName'");
$stmt->execute();
$array = [];
foreach ($stmt->get_result() as $row)
{
$array[] = $row['points'];
}
print_r($array);
THis is my current code:
<?php
header('Content-Type: text/html; charset=Windows-1250');
session_start();
$firstName = $_SESSION['firstname'];
$servername = "db.xxxx.gsp-europe.net";
$username = "xxxxxxxxxxxxx";
$password = "xxxxxxxxxxxxxx";
$dbname = "xxxxxxxx";
/// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// check if the user exist
$check = "SELECT * FROM `member_profile` WHERE user_id = '$firstName'";
$result = mysqli_query($conn,$check) or die(mysqli_error($conn));
$rows = mysqli_num_rows($result);
//if exist increse points with 1
if($rows>=1){
$sql = "UPDATE `member_profile` SET points = points + 1 WHERE user_id = '$firstName'";
if ($conn->query($sql) === TRUE) {
echo "Thingz created successfully";
} else {
echo "Error doing sum thingz: " . $conn->error;
}
}
// if don't exist create user with points 0
if ($rows == 0)
{
$query = "INSERT into `member_profile` (user_id, points) VALUES ( '$firstName' ,'0')";
$result = mysqli_query($conn,$query)or die(mysqli_error($conn));
$conn->close();
}
?>
What I need in the nutshell: In the end of file, will be a "echo" that will show the current value of "points" column with identificator "user_id". Thats all
Thanks for your time, I appreciate it !
You are getting the result, but aren't fetching the datas.
$stmt->get_result() returns a result set -> mysqli_result and to handle this, you need to call the method fetch_array() from that result.
change your code to :
$results = $stmt->get_result();
while ($row = $result->fetch_array(MYSQLI_ASSOC))
{
$array[] = $row['points'];
}
If you only want 1 result without using arrays, don't use arrays (yes, yes).
$results = $stmt->get_result();
while ($row = $result->fetch_array(MYSQLI_ASSOC))
{
$points = $row['points'];
}
I'm having some issues with my PHP script.
I have a PHP function which is stored in a PHP file, and I'm trying to run that PHP function from another script.
So to explain myself with code:
like.inc.php:
function post_exists($id) {
$host = "example";
$username = "example";
$password = "example";
$database = "example";
$connection = new mysqli($host, $username, $password, $database);
$id = $connection->real_escape_string($id);
$query = $connection->query("SELECT COUNT(`id`) AS `count` FROM `posts` WHERE `id` = '$id'");
while ( $row = $objQuery->fetch_object() ) {
if ( $row->count == 1 ) return true;
}
}
profile.php:
include ( 'like.inc.php' );
if (post_exists(70) === true) {
echo 'Exists!';
}
A post with ID 70 exists, so it should echo Exists! but instead it just crashes half of my page. So maybe it's not loaded correctly?
Any help would be highly appreciated!
Where you have $objQuery must by $query. Try with this code:
function post_exists($id) {
$host = "example";
$username = "example";
$password = "example";
$database = "example";
$connection = new mysqli($host, $username, $password, $database);
$id = $connection->real_escape_string($id);
$query = $connection->query("SELECT COUNT(`id`) AS `count` FROM `posts` WHERE `id` = '$id'");
while ( $row = $query->fetch_object() ) {
if ( $row->count == 1 ) return true;
}
}
You can write the function in a different way like so:
function post_exists($id) {
$host = "example";
$username = "example";
$password = "example";
$database = "example";
$connection = new mysqli($host, $username, $password, $database);
$id = $connection->real_escape_string($id);
// Instead of getting count, just get the row and
// do the count with PHP
$query = $connection->query("SELECT * FROM `posts` WHERE `id` = '$id' LIMIT 1");
if($query){
return $query->num_rows > 0;
}
return false;
}
<?php
$username = "username";
$password = "password";
$hostname = "localhost";
$database = "database";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db($database,$dbhandle)
or die("Could not select database");
$id = 0;
if(isset($_GET['Day'])){ $id = (int)$_GET['Day']; }
if(!$id){
$query = "SELECT * FROM `TimeTable`";
} else {
$query = "SELECT * FROM `TimeTable` WHERE `Day`='".$id."'";
}
$result = mysql_query($query);
$rows = array();
while($r = mysql_fetch_assoc($result)) {
$rows[] = $r;
}
or die(mysql_error());
print json_encode($rows);
?>
This code worked previously, but has now stopped, and is producing Parse error: syntax error, unexpected T_LOGICAL_OR in /Directory/TimeTable.php on line 27
I am also looking to add more parameters, (eg: Where Day = $id and Year = $Year )
while($r = mysql_fetch_assoc($result)) {
$rows[] = $r;
}
or die(mysql_error());
This is a syntax error, doesn't know what to do with that or die() statement. Change to this:
$result = mysql_query($query);
if (!$result) {
die('Error');
}
while(...) {
}
I have looked up the new mysql functions an have changed to mysqli.
<?php
$username = "username";
$password = "password";
$hostname = "localhost";
$database = "database";
$link = mysqli_connect($hostname, $username, $password, $database);
if(mysqli_connect_errno()){
echo mysqli_connect_error();
}
$id= 0;
if(isset($_GET['Day'])){ $id=(int)$_GET['Day']; }
$year = 0;
if(isset($_GET['Year'])){ $year=(int)$_GET['Year'];}
if(!$id){
$query = "SELECT * FROM TimeTable";
} else {
if (!year) {
$query = "Select * FROM TimeTable";
} else {
$query = "SELECT * FROM TimeTable WHERE Day=$id AND Year=$year";
}
}
$rows = array();
//Perform JSON encode
if($result = mysqli_query($link, $query)){
while($r = mysqli_fetch_assoc($result)){
$rows[] = $r;
}
}
print json_encode($rows);
?>
The below returns "\nQuery was empty" as I run it simply from my server with the URL in the Browser.
This is the PHP code:
<?
$databasehost = "server";
$databasename = "xxxx";
$databaseusername ="xxxx";
$databasepassword = "xxxx";
$query = "SELECT * FROM `Tailor`LIMIT 0 , 30";
$con = mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
mysql_select_db($databasename) or die(mysql_error());
$query = file_get_contents("php://input");
$sth = mysql_query($query);
if (mysql_errno()) {
header("HTTP/1.1 500 Internal Server Error");
echo $query.'\n';
echo mysql_error();
}
else
{
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);
}
?>
No need to use file_get_content and you have to put one white space after table name.
<?php
$databasehost = "server";
$databasename = "xxxx";
$databaseusername ="xxxx";
$databasepassword = "xxxx";
$query = "SELECT * FROM `Tailor` LIMIT 0 , 30";
$con = mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
mysql_select_db($databasename) or die(mysql_error());
$sth = mysql_query($query);
if (mysql_errno()) {
header("HTTP/1.1 500 Internal Server Error");
echo $query.'\n';
echo mysql_error();
}
else
{
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);
}
?>
Your query has a problem you need whitespace after tablename
SELECT * FROM `Tailor` LIMIT 0 , 30
first
$query = "SELECT * FROM `Tailor`LIMIT 0 , 30";
but you overwrite it
$query = file_get_contents("php://input");
// $query like aaa=ggg&bbb=kkk
then you
$sth = mysql_query($query);
// LIKE $sth = mysql_query('aaa=bbb&ccc=lll'); //it not sql query format
EDIT: I know the error is somewhere here:
$connection = #mysql_connect($server, $dbusername, $dbpassword) or die(mysql_error());
$db = #mysql_select_db($db_name,$connection) or die(mysql_error());
$sql = "SELECT * FROM authorize WHERE username = '$_SESSION[user_name]' and password = '$_SESSION[password]'";
$result = #mysql_query($sql, $connection) or die(mysql_error());
$num = mysql_num_rows($result);
$lstbalance = 0;
$balance = 0;
//set session variables if there is a match
if ($num != 0)
{
while ($sql = mysql_fetch_object($result))
{
$lstbalance = $sql -> lostbalance;
$balance = $sql -> balance;
}
}
if ($win==true)
{
$sql = "update users set lostbalance='($lstbalance+(($payouts[$result1.\'|\'.$result2.\'|\'.$result3])*(int)$_POST[\'bet\']))' WHERE username = '$_SESSION[user_name]' and password = '$_SESSION[password]'";
}
else
{
$sql = "update users set lostbalance='(lstbalance-(int)$_POST[\'bet\'])' WHERE username = '$_SESSION[user_name]' and password = '$_SESSION[password]'";
}
$result = #mysql_query($sql, $connection) or die(mysql_error());
I was able to narrow down the error to this piece of code, help appreciated. Regards.
When I comment it out everything seems to work all the connect variables are from a different file and are valid.
$lostbalance = $lstbalance+(($payouts[$result1])*(int)$_POST['bet']));
$sql = "update users set lostbalance='$lostbalance' WHERE username = '".$_SESSION['user_name']."' and password = '".$_SESSION['password']."'";
i dont understand about ur code on $payout[$result1.\'|\'.$result2.\'|\'.$result3]