PHP - Mysql Query Result Returns Always True!
soo i get info from a form and i want to search for it in my db .. if i find user_name AND user_password the $result returns true .. otherwise false ..
<?php require_once("../includes/connection.php"); ?> <?php $user_name=$_POST["user_name"]; $user_password=$_POST["user_password"]; ?> <?php $query = "SELECT * FROM users WHERE '{$user_name}' = user_name AND '{$user_password}'= user_password "; $result = mysql_query($query,$connection); if($result==false){ redirect("../index.php");} elseif($result==true){ redirect("../welcome.php"); } ?> <?php //close connection! mysql_close($connection); ?> but the problem is that it always returns TRUE !!!! whats the problem here exactly ??? Similar TutorialsHello. This one is driving me up the wall. It used to work, I have a sports league database (MsSql) and one of the tables contains team Captain's first name, last name, etc.. To create a unique but simple login, the query searches for the captain's first initial of their first name + lastname + league. However since there is the remote possibility of two similar usernames (i.e. David Smith) if the last name is found, the query adds an integer to the end of the last name. So the result would look like this: DSmithM4A At the moment, my database is completely blank. However what is happening when I run the query below is I get: DSmith1M4A Can someone figure out why this code is not working correctly? If there is not more than one "Smith" no integer is supposed to be returned. There is something in the "Captain Count" that I think is not working correctly. Code: [Select] /* * Gather captian information */ $strCaptainFirstInitial = substr($arrPost['cpt_first'],0,1); $strCaptainLastName = preg_replace("/[^a-zA-Z]/","",$arrPost['cpt_last']); $strCaptainName = strtolower($strCaptainFirstInitial.$strCaptainLastName); $sqlCaptainSearch = sprintf ( $strCaptainSearch, $strCaptainName ); $resCaptainSearch = mssql_query($sqlCaptainSearch,$conDB); /* * Create unique captain login and Password */ $strCaptainCount = (mssql_num_rows($resCaptainSearch) > 0) ? mssql_num_rows($resCaptainSearch) : "" ; $strLeagueTypeInitial = substr($arrLeagueSearch['type'],0,1); $strCaptainLogin = $strCaptainName . $strCaptainCount . $strLeagueTypeInitial . $arrLeagueSearch['size'] . $arrLeagueSearch['division']; I have a table korisnici in SQLite with INTEGER field aktivan that can have only 0 or 1 value (CHECK constraint). Field aktivan has value 0 but PHP returns value 1, why? Is this a bug? This is PHP code that I am running: $sql = "SELECT ime, aktivan FROM korisnici WHERE lower(ime) = '" . $ime . "'" . " AND sifra = '" . $_POST["sifra"] . "'"; $result = $db->query($sql); $row = $result->fetchArray(SQLITE3_ASSOC); $row['aktivan'] = 1 but in table the value is 0. When I run same query in DB Browser for SQLite I get correct value 0. Is this a bug? I'm trying to write a php page that displays data from a JOIN query for a specific ID table view brandinfo ID, brand, discounttype 1, antioni, no discount brandproducts brandID, producttype, price 1, Tshirt, 20.00 1, Pants, 30.00 1, Shoe, 40.00 the returned result is 1 antioni, no discount, Tshirt, 20.00, 2 antioni, no discount, Pants, 30.00 3 antioni, no discount, Shoe 40.00 The way I want the page to be displayed is ------------------ Antioni (at the top) Table 1. Tshirt 20.00 2. Pants 30.00 3. Shoe 40.00 no discount (at the bottom) ---------------------------- How should I construct the PHP page from the result since they're retrieved as rows? Hi, I have a sql query that executes perfectly in phpMyAdmin but when applied to a PHP script returns nothing. It won't even start a "while" loop. Please review code snippits below. Submission Script calls function in Database Class: $result = $database->testReturnId($_POST['years'], $_POST['model']); while($row = mysql_fetch_array($result)) { $i = $i + 1; echo "ROW " . $i . " " . $row['auto_id'] . " " . $row['auto_year'] . " " . $row['auto_year_high'] . " " . $row['auto_make'] . " " . $row['auto_model'] . "<br />"; } Database Class Function: function testReturnId($year, $model){ global $form; $q = 'SELECT * FROM tbl_svc_auto WHERE auto_model = "$model" AND "$year" BETWEEN auto_year AND auto_year_high'; if(!mysql_query($q, $this->connection)){ return false; }else{ return $result = mysql_query($q, $this->connection); } } I have moved the query to the submission script and replaced the variables with actual data and it still will not work. I can take the same query with out the variables and input it into phpMyAdmin and it works great. On the submission script, I added a row number to the echo thinking that it would a least return the word "ROW" and it does not. I have tested each step of the execution of the query and have not returned any errors. I do not receive any errors at all even during execution. It just seems to refuse to run the "while" statement. I do not know where my problem is......Please help. Thank you in advance for any assitance you can offer. Joshua <?php if('00' == '000') echo 'weired'; ?> Please consider the code above. Strangely it echoes the word "weired". Also if('00' == '000000000000000') returns true and so forth. What is really going on? Thanks in advance By the way the php version is 5.3.9 on wampserver 2.2 So in my last thread I was working on checking if a file was there...I resorted to something not in the library and used $pic_exists_jpg = fopen($jpg_path,'r'); $pic_exists_jpeg = fopen($jpeg_path,'r'); $pic_exists_gif = fopen($gif_path,'r'); $pic_exists_png = fopen($png_path,'r'); if ($pic_exists_jpg != '0') { $jpg_test = '1'; } elseif ($pic_exists_jpg == '1') { $jpg_test = '2'; } elseif ($pic_exists_jpg == '0') { $jpg_test = '2'; } elseif ($pic_exists_jpeg != '0') { $jpeg_test = '1'; } elseif ($pic_exists_gif != '0') { $gif_test = '1'; } elseif ($pic_exists_png != '0') { $png_test = '1'; } now for some reason php is being a poo and no matter what returning that there is a file there. no matter what method i use be it fopen or file_exists or anything else, it saying I have a file in that directory BUT I DO NOT! THERE IS NOT ONE OF THOSE FILES. I know I have my paths right because If I echo them directly I get the picture...what the heck?! hello I'm using this code: Code: [Select] $query="SELECT * FROM `second_content` WHERE CHANGED =0 limit 0,1";to query first row data when I want see it and echo it I recived : Code: [Select] Resource id #2can anyone help me ? thank you Hello - Suppose I had a MySQL result set that was something like this: ITEM: COLOR: ball red ball blue book red book green book black If I were to use the code: Code: [Select] do { echo $row_myQuery['item'] . " - " . echo $row_myQuery['color'] . "<br />"; } while ($row_myQuery= mysql_fetch_assoc($myQuery)); Then I end up with this: Code: [Select] ball - red ball - blue book - red book - green book - black MY QUESTION: What I'd like to know is, how could I re-write that php code so that I could get an output like this?: Code: [Select] ball - red, blue book - red, green, black Thanks! Hi Everyone. I get the following data from a result from database search <?php echo $rsjobs['CompanyURL'];?> It prints out the url but when I try to turn it into a clickable link it does not work Here is the coding I have so far. <a href="<?php echo $rsjobs['CompanyURL'];?>"><?php echo $rsjobs['CompanyURL'];?></a> What am I doing wrong ? please help. I have been trying to write code that deletes a book from a database if the user provides a BookName, and the corresponding DeletionCode. I then want to provide the user with feedback, telling the user whether they have deleted a book, that is they have provided a deletion code and a book name, or whether they have not deleted a book, because they have entered the wrong bookname or book ID. I thought this code would work, but it seems that the $result variable is always set as TRUE, no matter whether the code has deleted a row or not. What have i done wrong? $result = mysql_query("DELETE FROM books WHERE BookName = '$_POST[BookName]' && DeletionCode = '$_POST[Deletioncode]'"); if(!$result){ die('Invalid query: ' . mysql_error()); } else echo "delete book"; Why does this return null rather than 'john must write the book'? Code: [Select] <? abstract class User { protected $name; protected $role; function __construct($name,$role){ $this->name = $name; $this->role = $role; } abstract function run_errand(); } class Author extends User { public function run_errand(){ echo $this->name . "must write the book"; } } class Editor extends User { public function run_errand(){ echo $this->name . "must edit the book"; } } class Boss { private $users = array(); public function review(User $user){ $this->user[] = $user; } public function delegate(){ if(count($this->users)){ foreach($this->users as $user){ if($user->role == "author"){ echo $user->run_errand(); } } } } } //client code $boss = new Boss(); $boss->review(new Author('John','author')); var_dump($boss->delegate()); //returns null ?> Hi, it's my first post so hello to you and i hope you will be forgiving:) I've made a basic script, which takes a urls from the form(each of them is the new row) and then search each of those urls for an email. The problem is that only the email from the last url is returned. print_r("show link:". $urls[$i]."<br>"); shows all urls in the form, but the print_r("show current_url:". $current_url); shows only the last one. Code: [Select] <?php $urls = explode("\n", $_POST['urls']); $db = new mysqli('localhost', 'root', 'root', 'urls'); if (mysqli_connect_errno()) { echo 'Błąd: '; exit; } for ($i=0; $i<count($urls); $i++){ print_r("show link:". $urls[$i]."<br>"); $current_url = file_get_contents($urls[$i]); print_r("show current_url:". $current_url); preg_match( "/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $current_url, $email);//email print_r ("show email:".$email[0]); $zapytanie = "INSERT INTO urle set adres = '$email[0]' "; $wynik = $db->query($zapytanie); } if ($wynik) { echo $db->affected_rows ."pozycji dodano."; } else { echo mysql_errno() . ":" . mysql_error() . "Wystąpił błąd przy dodawaniu urli "; } $db->close(); ?> this is supposed to display the distinct first two characters of a string(billnmbr) in a combobox ive tried this on phpmyadmin and it returns 08.. which is correct when i run this on my server, it returns nothing.. there must be something wrong with the way i called the billnmbr does anybody have an idea on how i could call and display this correctly? thanks and have a nice day. <?php $mquery = "select distinct(substring(`billnmbr`, 1, 2)) from `tbltest`"; $mres =mysql_query($mquery) or die(mysql_error()); while($mrow=mysql_fetch_array($mres)){ $mm = $mrow['billnmbr']; echo "<option value=" . $mrow['billnmbr'] . ">"; echo $mrow['billnmbr'] . "</option>"; } echo " </select>"; ?> Hi all ! The following code has two similar queries. Query 1 and Query 2. While Query 1 works just fine, Query 2 simply fails persistently. I have not been able to figure out why?
I have checked every single variable and field names but found no discrepancy of any sort or any mismatch of spelling with the fields in the database.
I would be grateful if anybody can point out what is preventing the second query from returning a valid mysqli object as the first one does. Here is the code for the two.
<?php $db_host = "localhost"; $db_user ="root"; $db_pass =""; $db_database ="allscores"; //////////////// CHECKING VARIABLE FILEDS IN A UPDATE QUERY ////////////////////////////////// //////////////// QUERY 1 ///////////////////////////////////////////////////////////////////// /* $db_database ="test"; $db_table ="users"; $RecNo = 1; $field1 = 'name'; $field2 = 'password'; $field3 = 'email'; $field4 = 'id'; $val1 = "Ajay"; $val2 = "howzatt"; $val3 = "me@mymail.com"; $con = mysqli_connect($db_host,$db_user,$db_pass,$db_database) or die('Unable to establish a DB connection'); $query = "UPDATE $db_table SET $field1 = ?, $field2 = ?, $field3 = ? WHERE $field4 = ? "; // session terminated by setting the sessionstatus as 1 $stmt = $con->prepare($query); var_dump($stmt); $stmt->bind_param('sssi',$val1,$val2,$val3,$RecNo); if($stmt->execute()) { $count = $stmt->affected_rows; echo $count; } //////////////// QUERY 1 END ///////////////////////////////////////////////////////////////////// */ //////////////// QUERY 2 ///////////////////////////////////////////////////////////////////// $con = mysqli_connect($db_host,$db_user,$db_pass,$db_database) or die('Unable to establish a DB connection'); $table = 'scores'; $date = date('Y-m-d H:i:s'); /* $prestr = "Mrt_M_"; $STATUS = $prestr."Status"; $S_R1 = $prestr."Scr_1"; $S_R2 = $prestr."Scr_2"; $PCT = $prestr."PPT"; $DPM = $prestr."DSP"; $TIMETAKEN = $prestr."TimeTaken"; */ $STATUS = "Mrt_M_Status"; $S_R1 = "Mrt_M_Scr_1"; $S_R2 = "Mrt_M_Scr_2"; $PPT = "Mrt_M_PPT"; $DSP = "Mrt_M_DSP"; $TIMETAKEN = "Mrt_M_TimeTaken"; $TimeOfLogin = $date; $no_of_logins = 10; $time_of_nLogin = $date; $m_scr_row1 = 5; $m_scr_row2 = 5; $m_ppt = 20; $m_dsp = 60; $m_time = 120; $date = $date; $RecNo = 24; $query = "UPDATE $table SET TimeOfLogin = ?, no_of_logins = ?, time_of_nLogin = ?, $S_R1 = ?, $S_R2 = ?, $PPT = ?, $DSP = ?, $TIMETAKEN = ?, $STATUS = '1', TimeOfLogout = ?, WHERE RecNo = ?"; $stmt = $con->prepare($query); var_dump($stmt); $stmt->bind_param('sisiiddssi',$TimeOfLogin,$no_of_logins,$time_of_nLogin,$m_scr_row1 $m_scr_row2,$m_ppt,$m_dsp,$m_time,$date,$RecNo); if($stmt->execute()) echo " DONE !"; ?>Thanks to all Hey Everyone, I want to start out by saying thanks in advance for reading this. Since I started working with the XAMPP package this community has been a great resource, though this is my first post. most of my work has been modifying existing stuff in our network, this is my first project from scratch. I am working on an access control project where I am storing site names and RFID tags in a table, and using PHP to check for existing records. Basically, I want to submit 2 credentials from a host (site and RFID), check if they exist together in the table, if so return TRUE or 1, if not return FALSE or 0. I am new to both PHP and MySQL (although I took a course years ago) so I am looking here for help. On this server I have XAMPP installed and everything I need running. I have a table and created a few records for testing. My current trouble is getting PHP to return the proper values for the SQL query. The SQL query works fine, I think - it returns the number of rows that match and that record. I'm not sure this is the result I need to be able to deal with the way I want. Using other resources my current PHP statement looks like this: Code: [Select] if(mysql_num_rows(mysql_query("SELECT * FROM `users` WHERE 'site_name' LIKE 'berland' AND 'card_id' LIKE '290093C84E' LIMIT 0 , 30"))>0){echo '1';} else {echo '0';} So, my table has the entry as noted in this query above. Final one will be dynamic where the host will be sending those values - I will likely need help with that as well, but small steps first.;-) Anyway, I was playing around with the end statement >0{echo '1';} but wasn't able to make it do what I want. Any help is appreciated. Thank you. Aaron Hello all. I am using a premade login script which has some very powerful function built in, the one I am looking at using is as follows:
/** * query - Performs the given query on the database and * returns the result, which may be false, true or a * resource identifier. */ function query($query){ return mysqli_query( $this->connection, $query); }The issue is, displaying the results. I call is via the following code: $database->query('SELECT LastName, Firstname FROM Patrons WHERE LastName = "Delude"');But I have no idea how to display it. I have attempted to echo it out, but I get the following error in the logs: PHP Catchable fatal error: Object of class mysqli_result could not be converted to string Hello, So I have a content management site set up, but I'm stuck on one part. I have a query. Code: [Select] Select `email` FROM author WHERE author.id IN(SELECT authorid FROM job WHERE id = '$_POST[id]') I'm a beginner in PHP and SQL. Basically I need the query to run, it will come out with one result and I need that to be where the email is sent to. This is the existing code I have for sending out the email $subject = " Job Application Confirmation"; $message = "Hello $_POST[name] , \r\rYou, or someone using your email address, has applied for the job: $text Using the Resource Locator at Prahan.com. Keep this as future reference. \r\r Full Name: $_POST[name] \r Location: $_POST[location] \r Email: $_POST[email] \r Additional Information: $_POST[info] \r Job ID: $id \r Job Name: $text \r \r Expect a response shortly. \r\r Regards, Prahan.com Team"; $headers = 'From: Prahan.com Team <noreply@prahan.com>'; 'Reply-To: noreply@prahan.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); $to1 = 'pratik@prahan.com'; mail($to1, $result, $message, $headers); Thanks in Advance! Hi there
I am new to this forum and not much of a php expert. I hope you can assist me.
My problem is as follows:
I have a column consisting of several values (in this case: sceience, art, humanistics and music)
I would like to use the following mysqli query in order to count the appearances of each value in this column:
$query = "SELECT SUM (IF(department = 'science', 1, 0)) AS science, SUM(IF(department = 'art', 1, 0))-> AS art FROM new2"
I have tried lots of ways to loop the results but couldn't get the it working.
Can anyone suggest any idea?
Thanks
Hanan
|