check if row mysql exist with php - php

i'm trying to check if a row exist in my database using php, and the problem is that i can't understand what i wrong, this is the code:
while ($db_field = mysql_fetch_assoc($result))
{
print "||" . $db_field['id_n']."||".$db_field['network_name']."||".$db_field['country']."||".$db_field['country_Name']."||"."<BR>";
$query = "SELECT * FROM countries WHERE english_name='$db_field['country_Name']'";
$doquery = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($doquery))
{
print 'Found';
} else {
print 'Not Found';
}
}
if i write these i receive no output, and also no error, i try to insert some print in the code at the start of my php or in the middle but no print is displayed, so i found that the error is in this line:
$query = "SELECT * FROM countries WHERE english_name='$db_field['country_Name']'";
my question is, what is the error, and why display anything, i'd like it display an error or something else, or just print me the log i put in the code but nothing, seems that the php is blank, anyone can explain me these please?

Related

What is the difference between the two? (query select and display result)

I am trying to understand what the difference is between these two lines of code.
I have two pages ACR.php and webPage1.php. ACR.php is included on any page I need to have a database connection. The goal is to obtain the value from the database and display it on the webpage.
// ACR.php //
<?php
// Est. Connection
$dbc = mysqli_connect($ACR_host, $ACR_user, $ACR_pass, $ACR_tablename)
or die('Error communicating to MySQL server');
// Select value from row in table
$thisPort = $dbc->query("SELECT activePort FROM table1 ")->fetch_row()[0];
// Select value from row in table
$thatPort = "SELECT activePort FROM table1";
// Displays result
$result1 = mysqli_query($dbc, $thisPort);
$result2 = mysqli_query($dbc, $thatPort);
?>
webpage1.php will not display the correct value when echoing $result1 but rather echo 'result not found'.
// webPage1.php //
<?php include 'ACR.php';?>
<!--html-->
<tr>
<th>Port<span id="portDisplay"></span><sup></sup>:</th>
<td id="showPort" style="text-align:left;width:75%;">
<?php
session_start();
if (mysqli_num_rows($result1) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result1)) {
echo " " . $row["activePort"];
// echo $row;
}
} else {
echo "result not found";
}
mysqli_close($dbc);
?>
</td>
</tr>
However, if I echo in $result2, the correct value displays from the database table.
Why is this? What makes this so? Are not both $thisPort and $thatPort calling the same row in the table?
UPDATE: I use $thisPort, in part, when fetching a local and remote address. So i would prefer using the current syntax for $thisPort as it works but changing it to the syntax of $thatPort crashes the page. Why is it the value can be pulled from the Database when using this method, but not when i need to display the port on the page.
I don't want to initiate an extra variable ($thatPort), if I don't have to, to simply echo the value from the database when the original var ($thatPort) should and is already doing this.
$thisPort already contains a row from the database. This is not a valid argument to supply to mysqli_query. If you look at your php error log, you'll probably see (depending on your log settings) that the line $result1 = mysqli_query($dbc, $thisPort); generates an error message; $result1 will actually contain FALSE.
$thatPort contains a SQL query string, which is a valid argument to the mysqli_query function, and creates a valid result set in $result2.

PHP If Else statement not working for database

Ok, I'm confused. I have some code that searches a database table for a username, and then uses an if else statement to run some code depending on if the user is found or not. My code is below. The problem is that the code isn't even seeing the if else statement, and I have no idea why. Any help is appreciated.
$sqluser = "select * from users where username='" . $user ."'"; //Searching to see if the user is in the database
echo $sqluser . "<br><br>"; //writes out the select statement to make sure it is correct
$query = mssql_query($sqluser); //returns the results
$num_rows = mssql_num_rows($query); //gets the number of rows returned
echo $num_rows; //writes out the number of rows
if ($num_rows==0) //determines what happens next if the user exists or not
{
//displays an error box if the user doesn't exist
echo "<script type=text/javascript>";
echo "alert('That user doesn't exist. Please try again.')";
echo "</script>";
}
else
{
//will be code to run if the user does exist
echo "<script type=text/javascript>alert('Testing.')</script>";
}
I couldn't add a comment. So I will write this as an answer instead.
Since you state that the alert JavaScript is showing in the page source, this mean that the IF/ELSE statement in PHP is working fine. The problem is with the single quote. You have a single quote inside a single quoted alert function. Hence the JavaScript alert function cannot be executed.
echo "alert('That user doesn't exist. Please try again.')";
Try using this instead
echo "alert('That user doesn\'t exist. Please try again.');";

background working updating script not working

I created an background working script, that updates my database table on a condition.
Here is my main script:
<?php
require_once('conn.php');
$query = "SELECT * FROM user WHERE id = '" . $_COOKIE['username'] . "'";
$data = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($data);
if($row['ehp'] > $row['required_ehp']) {
// The 'ehp' column is greater than 'required_php'
$calc = $row['ehp'] % $row['required_ehp'];
$new = $row['required_ehp'] * 12;
$new_lvl = $row['level'] + 1;
$query2 = "UPDATE user set level = '$new_lvl', ehp = '$calc', required_ehp = '$new'";
mysqli_query($dbc, $query2);
echo 'query successful';
} else {
echo 'query unsuccessful' . mysql_error();
}
echo $row['ehp'] % $row['required_ehp'];
echo $row['required_ehp'];
?>
The error message I am getting from this script is:
query unsuccessful
Warning: Division by zero in H:\AppServ\www\sp\userlevel.php on line 21
I don't know what's is wrong. Please help me.
Here is the database column's image.
Since you see query unsuccessful, that means that the line if($row['ehp'] > $row['required_ehp']) is false.
In the line echo 'query unsuccessful' . mysql_error(); there is no mysql error, therefore you only see the line query unsuccessful.
Also you get the devision by 0 warning. This is caused by the line echo $row['ehp'] % $row['required_ehp'];. It seems $row['required_ehp'] is 0.
Summing this all up, perhaps the data you expect to be in $row is not what you are expecting. Maybe the cookie data is incorrect?
Also, putting the cookie data straight into the query is a horrible idea, and easily to hack.
Okay I found out my mistake it was just that in id in my database, I was searching for the username column. Sorry to bother you all.

Fastest way to get MySQL cell data as a string?

I want to take the value of a single MySQL cell and use it as a string inside PHP code - I already know the cell exists, where it is, and nothing else is needed. What's the easiest way to do this? All the examples I've found focus on using a loop to output multiple rows into a table, which seems needlessly complicated for my purposes.
Basically what I want to do is this:
require_once 'login.php'; // Connects to MySQL
$sql = "SELECT name FROM users WHERE id='1'"; // id is determined elsewhere
$result = mysqli_query($connect, $sql);
echo "Your name is " . $result;
But I get an error message that it's not a valid string.
You forgot to fetch record from $result using mysqli_fetch_assoc().
So you can fix your code this way:
$result = mysqli_query($connect, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo "Your name is " . $row['name'];
}

mysqli_fetch_assoc doesnt return first row

sorry I am very new to PHP. This has probably been asked before but I am unable to get this code working, I am currently trying to check whether a user has logged in before but the mysqli_fetch_assoc only seems to pull through the second row, this query should only return one row so i only figured it out it was returning two when i added a duplicate to my DB. I have found similar questions and solutions but havent been able to figure it out.
Thanks in advance.
require_once 'login.php';
$db_server = mysqli_connect($db_hostname,$db_username,$db_password,$db_database);
$query = "SELECT * FROM users WHERE username = 'ben944' AND firstlogin = '0'";
$result = mysqli_query($db_server,$query);
if(!$result) {
echo "not working";
exit;
}
$row = mysqli_fetch_row($result);
while($row = mysqli_fetch_assoc($result))
{
print_r($row);
}
The separate $row = mysqli_fetch_row($result); line already fetches and removes the first row from the result before the while loop starts. That's why it's not printed by the while loop (which you have added for debugging purposes obviously).

Categories