mysqli_fetch_assoc doesnt return first row - php

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).

Related

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'];
}

Fecthing information from mysql

I really got a problem now. I tried for decades and I can't find why it is not working. I want to get the user that is logged in to see their email on a specific page. I tried a new code now and i get this error: Notice: Undefined variable: row in
The code I use is:
<?
$username = $_SESSION['username'];
$sql = "select * from users where username=" . $username . "";
echo $sql;
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
}
?>
AND
<?php echo $row['email']; ?>
<?php
$username = $_SESSION['username'];
$query = mysql_query("select * from users where username='".$username."'");
while ($row = mysql_fetch_array($query)) {
$email=$row["email"];
}
echo $email;
?>
try this.
don't use mysql_* functions
I think... Problem is in SQL query. I propose your column "username" is something like VARCHAR(50). So you have to add quote.
$sql = "select * from users where username='" . $username . "'";
I see a bug, and a design problem.
You've designed your script so that you're printing whatever was last assigned to $row in the condition of your while loop.
You're getting the error because the query is not returning anything and the loop is not running. Therefore, $row is never assigned. That being said, you probably don't want to use a while-loop if all you're trying to do is display the value of the "email" column in the first record returned. If you did want to, then stop it.
Call mysql_fetch_assoc() on your $result (doesn't return as much data), and check that it doesn't return FALSE (one or more records weren't found).
if((row = mysql_fetch_assoc($result)) === false)
die("Error.");
?>
Email:

got stuck converting MySql output in JSON array with php

I am making an iphone app. a part of that app allows users to search for a company on location.
I have a MySql database containing the companies that can be searched for, and a php file on my website to receive the searched data, and to return the companyName and companyLocation for all the found companies to my app. it looks like this:
<?php
if (isset($_GET["companyCitySearchField"])){
$companyCity = $_GET["companyCitySearchField"];
$result = search($companyCity);
echo $result;
}
function makeSqlConnection()
{
$DB_HostName = "******";
$DB_Name = "*******";
$DB_User = "*******";
$DB_Pass = "*******";
$con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die(mysql_error());
mysql_select_db($DB_Name,$con) or die(mysql_error());
return $con;
}
function disconnectSqlConnection($con)
{
mysql_close($con);
}
function search($companyCity)
{
$con = makeSqlConnection();
$query = mysql_query("SELECT companyName, companyCity from Company WHERE companyCity = '$companyCity'");
$companies = array();
while ($row = mysql_fetch_assoc($query)) {
$companies['companies'][] = $row;
print json_encode($companies);
}
disconnectSqlConnection($con);
}
?>
this works fine when only one company is found. it gives me a perfect JSON array:
{"companies":[{"companyName":"Dijkstra","companyCity":"Geldermalsen"}]}
everything fine so far.
Now, I create another company in my database, also with Geldermalsen as location.
2 companies are found in the database now. the JSON array it return now, doesn't make sense:
{"companies":[{"companyName":"Dijkstra","companyCity":"Geldermalsen"}]}{"companies":[{"companyName":"Dijkstra","companyCity":"Geldermalsen"},{"companyName":"testaccount","companyCity":"Geldermalsen"}]}
for some reason, it seems to make 2 separate array's. one for the first found company, and one with both.
I have been searching the web, stackoverflow, google and even the book 'PHP and MySql for dummies' for days, and I have changed my code numerous times, and whatever I try it keeps on doing this.
Does anyone know what I should do to get one array containing all found companies with this script, instead of these 2?
any help would be very welcome, Thank you in advance!
You are echoing out JSON for each row, not for the fully built array. Move your print statement outside the loop.
$companies = array();
while ($row = mysql_fetch_assoc($query)) {
$companies['companies'][] = $row;
}
print json_encode($companies);
Or better yet, you might not want to echo out anything at all in the search function, but leave that up to the caller. It seems you already might be intending to do this here:
$result = search($companyCity);
echo $result;
The only problem is that the search() function doesn't return any value so $result would be null. You should make up your mind about where you are going to echo the result to the client and be consistent about it.

check if row mysql exist with 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?

php mysqli help, first line in DB not being returned?

Here is my code
<?php require_once 'connect.php';
$sql = "SELECT * FROM `db-pages`";
$result = $mysqli->query($sql) or die($mysqli->error.__LINE__);
while ($row = $result->fetch_assoc()) {
echo($row['pagetitle'].' - To edit this page click here<br>');
}
}
?>
I've added a couple more rows to the Database and it's returning them all, apart from id=1 in the DB. Any idea why?
try it like this:
while ($row = $result->fetch_assoc()) {
echo($row['pagetitle'].' - To edit this page click here<br>');
}
Double check the title and ensure it's got nothing that will affect php out-putting it.
Also escape all of your DB output using htmlentities, it makes for good practise in the event someone gets creative.

Categories