Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to call SQL statements from a php function
    primarykey
    data
    text
    <p>I'm trying to write a code where all SQL statements are stored inside a separate PHP file so I can call them later as functions.</p> <p>I've tried different things but I can't seem to make it work properly. </p> <p>Here is my code (I'm trying to make a login page here)</p> <p>login.php</p> <pre><code>&lt;?php include('sql.php'); $sql = new sql(); ?&gt; &lt;form class = "form-inline" method = "post" style="color: #FFF; position: absolute; margin-top:100px; margin-left:500px;"&gt; &lt;table width="100%" border="0"&gt; &lt;tr&gt; &lt;td&gt;Username&lt;/td&gt; &lt;td&gt;&lt;input type="text" id = "username" name = "username" class="large" placeholder=""&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Password&lt;/td&gt; &lt;td&gt;&lt;input type="password" id = "password" name = "password" class="large" placeholder=""&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;br /&gt; &lt;center&gt;&lt;button type="submit" id = "submit" name = "submit" class="btn btn-primary"&gt;Log in&lt;/button&gt;&lt;/center&gt; &lt;/form&gt; &lt;?php //echo $sql-&gt;admin(); ?&gt; &lt;?php if(isset($_POST['submit'])) { // username and password sent from form $username=$_POST['username']; $password=$_POST['password']; // To protect MySQL injection (more detail about MySQL injection) $username = stripslashes($username); $password = stripslashes($password); $username = mysql_real_escape_string($username); $password = mysql_real_escape_string($password); //THIS IS THE PART WHERE I CALL THE SQL STATEMENT FROM A SEPARATE PHP FILE echo $sql-&gt;admin($username,$password); // Mysql_num_row is counting table row $count=mysql_num_rows($sql); // If result matched $username and $password, table row must be 1 row if($count==1) { //unset($_SESSION); $row = mysql_fetch_assoc($sql); $_SESSION[logged] = $row[logged]; // Register $username, $password and redirect to file "index.php" $_SESSION['username'] = $username; $_SESSION['password'] = $password; $_SESSION['submitted'] = $row[submitted]; $_SESSION['date_submitted'] = $row[date_submitted]; session_register($_SESSION['username']); session_register($_SESSION['password']); ?&gt; &lt;script&gt;window.location="index.php";&lt;/script&gt; &lt;?php } else { ?&gt; &lt;center&gt;&lt;span style="color:white;"&gt;Wrong Username or Password&lt;/span&gt;&lt;/center&gt; &lt;?php } } ?&gt; </code></pre> <p>sql.php</p> <pre><code>&lt;?php include('connect.php'); class sql{ function sql() { } function admin($username,$password) { echo $sql=mysql_query("SELECT * FROM admin WHERE admin_username= '$username' and admin_password= '$password' "); } } ?&gt; </code></pre> <p>The error that appears with this code is</p> <pre><code>Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in login.php on line 40 </code></pre> <p>my line 40 is</p> <pre><code>$count=mysql_num_rows($sql); </code></pre> <hr> <p>I've finally got it. As you said, I've added a line in my sql.php which is </p> <pre><code> return $result; </code></pre> <p>another reason why my code didn't work was because I didn't place my </p> <pre><code> $sql-&gt;admin($username,$password); </code></pre> <p>inside a variable. Now it looks like this </p> <pre><code>$a = $result-&gt;admin($username,$password); </code></pre> <p>and it's working now.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload