douganbi7686 2013-10-25 01:20
浏览 103
已采纳

PHP在sql查询中使用变量

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;
?>
  • 写回答

1条回答 默认 最新

  • ds3016 2013-10-25 01:38
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题