mysql_fetch_assoc in MeekroDB - mysql

Excuse me for being new to MeekroDB library, I wonder how to check for rows in the login process;
$query = DB::query("SELECT * FROM users WHERE username=%s",$username);
$numrows = DB::count($query);
// this needs the edit
if ($numrows!=0){
while($row = mysql_fetch_assoc($query)){
$dbusername = $row['username'];
$dbpassword = $row['password'];}

You need to bear in mind that your variable $query is now an array (if any records are found) so you do not need to do
while($row = mysql_fetch_assoc($query))
Instead try
foreach ($query as $user)
{
$dbusername = $user['username'];
$dbpassword = $user['password'];
}
Also, if you have more then one record with the same username and password then you may be doing something wrong with your user registration/maintenance process.

Related

Last Record from Mysqli Database not showing

Thank you in advance for your help.
For some reason I cannot get the last record from the Database to show when I retrieve information from it.
This was working just fine until I upgraded from PHP 5 to PHP 7.1
<?php
$servername = "xxxxxxxx";
$username = "xxxxxxxx";
$password = "xxxxxxxx";
$dbname = "xxxxxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Second Half
$sql="SELECT * FROM Online_Customers order by id DESC LIMIT 100";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
fetch_assoc() removed in PHP 7
change
$row = $result->fetch_assoc()
to
$row = $result->fetch_array()
I got this fixed. I removed the if statement and it's working just fine. If someone is having the same problem please let me know and I can guide you through.

overwrite existing data based on returned values

Trying to overwrite existing data based on returned values, just want to get 'location' to update to the new info coming from a CSV import. Have a duplicated table with Oldlocation (all set to DW for testing, so any changes can be seen) Seems like it should be simple....:
$file = "allinv.CSV";
if(($handle = fopen($file, "r")) !== FALSE)
{ fgetcsv($handle);
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE){
$num = count($data);
for ($c=0; $c < $num; $c++) {
$col[$c] = $data[$c];}
$col1 = $col[1];
$sql = "SELECT Oldlocation FROM invbkup WHERE VIN = '$col1'"; ///works fine, gets old location for each record
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_array($result);
$whl= $row['Oldlocation'];
$col8 = $col[73];
$query = "UPDATE allinv SET location = '$col8' WHERE $whl = 'DW' ";
echo $col1, $col8, $whl, "</br>";////can see all the info but get no changes in database.
$a= mysqli_query($con, $query);
}
fclose($handle);
}
Nevermind, it was a simple problem I fix by putting it in an if statement:
if($whl = 'DW'){
$query = "UPDATE allinv SET location = '$col8' WHERE VIN ='$col1'";
$a= mysqli_query($con, $query);}
sometimes you over complicate the problem yourself, take a break, drink some coffee and try not thinking so hard.

select query will not execute, is my syntax wrong?

Apparently i have something wrong because this query will not execute, Can anyone see why ?
$username=username;
$mysqli = #new mysqli($localhost, $user, $pass, $database);
$sql = "SELECT * FROM player_log WHERE user = '$username'AND log_status=online";
$result = $mysqli->query($sql);
if ($result->num_rows > 0){
echo "its works";
}
Multiple issues see modified code below
Added standard way to call mysqli (space after $username is not an issue)
$username=username;
$mysqli = #new mysqli($localhost, $user, $pass, $database);
$sql = "SELECT * FROM player_log WHERE user = '$username'AND log_status='online' ";
if ($result = $mysqli->query($sql)){
printf("Select returned %d rows.\n", $result->num_rows);
/* free result set */
$result->close();
echo "its works";
}

Creating Json file from mysql

i can't get more than one return in this json. when the original query returns 90k results.
i can't figure out what's hapening.
also the return i get isn't organized as it should. it return the following
{"material":["R8190300000","0"],"grid":["R8190300000","0"]}
sorry to ask this i have been looking for an answer but couln't get it in the internet.
<?php
$link = mysqli_connect("localhost","blablabla","blablabla","blablabla");
if (mysqli_connect_error()) {
die("Could not connect to database");
}
$query =" SELECT material,grid FROM ZATPN";
if( $result = mysqli_query( $link, $query)){
while ($row = mysqli_fetch_row($result)) {
$resultado['material']=$row;
$resultado['grid']=$row;
}
} else {
echo"doesnt work";
}
file_put_contents("data.json", json_encode($resultado));
?>
The problem is that you are overriding the value for the array keys:
$resultado['material']=$row;
$resultado['grid']=$row;
At the end you will have only the last 2 rows; I suggest you to use something like:
$resultado['material'][] = $row;
$resultado['grid'][] = $row;
This will save you pair rows in $resultado['grid'] and unpaired rows in $resultado['material'];
After the information in comments you can use this code:
$allResults = array();
while ($object = mysqli_fetch_object($result)) {
$resultado['id'] = $object->id;
$resultado['name'] = $object->name;
$resultado['item'] = $object->item;
$resultado['price'] = $object->price;
$allResults[] = $resultado;
}
file_put_contents("data.json", json_encode($allResults));

Send an email using php with mysql data included

I'm trying to send a list of recent entries into a mysql table via email once a week using cron jobs. Typically to call the list of recent entries I use this:
$result = mysql_query("SELECT * FROM stock WHERE PurchaseDate < '$TODAY' AND PurchaseDate > '$LASTWEEK'")
or die(mysql_error());
while ($list = mysql_fetch_array($result))
But obviously I can't put this code into the $message variable in php mail.
Any ideas?
$result = mysql_query("SELECT * FROM stock WHERE PurchaseDate < '$TODAY' AND PurchaseDate > '$LASTWEEK'") or die(mysql_error());
$entries = 'Entries: ';
while ($list = mysql_fetch_array($result)) {
$entries .= $list[entry] . ', ';
}
mail('someone#test.com', 'Stock', $entries);
This is just an example. Not sure what your table looks like.
The mail functions takes a string, and you are returning an array.
So you need to implode it implode(',', $list); or build a string with the result set.
You should use PHPMailer, Zend Mail or Swift_Mailer libraries that are safer and prevent for example from header injection.
try {
$result = mysql_query("SELECT * FROM your_table WHERE blank = 'blank' AND blank2 ='blank2'");
$num_rows = mysql_num_rows($result);
if($num_rows < 1) {
throw new Exception('There is no user who qualifies...');
}
if(!$result) {
throw new Exception('An Error Occurred..');
}
//mail off whatever you need to each user that qualifies based on your query criteria..
while($row = mysql_fetch_array($result)) {
$firstname = stripslashes($row['firstname']);
$lastname = stripslashes($row['lastname']);
$email = stripslashes($row['email']);
//and any other variables you need for the email...
$subject = 'Some Subject';
$message = 'Hello '.$firstname.' blah..blah...';
mail($email, $subject, $message);
//do something else...
echo 'All users emailed.';
}
}
catch (Exception $e) {
echo $e->getMessage();
}