简体   繁体   中英

PHP Using variable within sql query

My include file includes all of my connection information and I use this for other pages to connect to my database.

I have commented out my working echo line where this shows me the results from my previous page these results come across fine at this point. The problem I am having is getting the results of my queries to display in line across the page while using the variable from the previous page as the where clause for my SQL statement. When using this where clause the results will only bring back 1 result. Does anyone have any suggestions?

I have tried adding and removing ' and " to different portions of the query and I am not having any luck. If necessary I can edit and add the page that shows up before this however currently that information is coming across in the line with the commented code on the predictions.php page.

Below is my includes.php

<?php
$dbaddress="localhost";
$dbuser="testuser";
$dbpass="testpass";
$dbname="testdb";
$dbtable="testtable";
$con=mysqli_connect($dbaddress,$dbuser,$dbpass,$dbname)
?>

Below is my predictions.php

<?
    include 'includes.php';
    if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
    $elousera=$_POST['elousera'];
    $elouserb=$_POST['elouserb'];
    #echo 'You selected sequence# '.$elousera.' vs sequence# '.$elouserb;
    $sqla = "SELECT mcacctname FROM $dbtable WHERE Sequence = '$elousera'";
    $resulta = mysqli_query($con,$sqla);
    $playera = mysqli_free_result($resulta);
    $sqlb = "SELECT mcacctname FROM $dbtable WHERE Sequence = '$elouserb'";
    $resultb = mysqli_query($con,$sqlb);
    $playerb = mysqli_free_result($resultb);
    echo 'You selected '.$playera.' vs '.$playerb;
?>

mysqli_free_result doesn't actually give you a row from the result. Consider:

    $playera = mysqli_fetch_array($resulta);
    ...
    $playerb = mysqli_fetch_array($resultb);

And $playera['mcacctname'] should give you the result if there is one. If not, $playera will be null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM