dongzhao1930 2015-11-07 20:02
浏览 65

如何从多维sql数据库中获取数据?

I have a database which I call datenbank in this example... In this database I have a table called "artikel".. I now have a number and want to search my table "artikel" in the column "idartikel" for this number; Once I found the right row I want to compare the entry of another column called kategorie_idkategorie of this row but of another column with a certain value;

To do so I start connecting to my database

error_reporting(E_ALL);
define ( 'MYSQL_HOST',      'localhost' );
define ( 'MYSQL_BENUTZER',  'root' );
define ( 'MYSQL_KENNWORT',  '' );

define ( 'MYSQL_DATENBANK', 'datenbank' );

    $db_link = mysqli_connect (
                 MYSQL_HOST,
                 MYSQL_BENUTZER,
                 MYSQL_KENNWORT,
                 MYSQL_DATENBANK
                );

    if ($db_link === False) {
        die("<p> Error </p>");
    }


?>

Diese connection works; Now I want to search for my numer, in this case 55065 in the table artikel and the column idartikel; Once I found the row I want the entry of the column kategorie_idkategorie and compare it to the value 1592

<?php


require_once ('configdatenbank.php');
$id=55065; //Diese Nummer möchte ich in meiner Tabelle artikel suchen


$wert1 = 1592;


$sql = "SELECT kategorie_idkategorie FROM artikel WHERE idartikel=55065";
$query = mysql_query($sql);
$row = mysql_fetch_row($query);
if($row['kategorie_idkategorie'] == $wert1){
  echo 'equal';
}else{
 echo 'not equal';
}

?> 

For a better understanding I made a screenshot of the entry of the table;

http://www.bilder-upload.eu/show.php?file=bed499-1446912607.png

As you see there is the entry where the column idartikel has the value 55065; Now I want to check if this row has the value 1592 in the colummn kategorie_idkategorie, which is obviously true; So I should get the message "equal";

However I get several errors when doing so and also get the message "not equal"

If you look at the database you see three different parts of each column: Funktion, Null and Wert; Is it the problem that I point on the column in general and not specifically on the "wert" part of the column?

Or what is the problem in this case?

Many thanks!! :)

Edit: My new version...But it's still not working

<?php


require_once ('configdatenbank.php');
$id=55065;


$wert1 = 1592;

// MySQL-Query zum holen des wertes aus der DB
$sql = "SELECT kategorie_idkategorie FROM artikel WHERE idartikel=55065";
$query = mysqli_query($sql);
$row = mysqli_fetch_assoc($query);

if($row['kategorie_idkategorie'] == $wert1){
  echo 'werte sind gleich';
}else{
 echo 'werte sind nicht gleich';
}

?> 
  • 写回答

1条回答 默认 最新

  • dsij89625 2015-11-07 20:10
    关注

    I had to edit, because jeroen pointed out that your connection is mysqli. Thats correct, you cannot mix the two, so upgrade from mysql to mysqli:

    And to fix your code, use fetch_assoc instead of fetch_row so that you can index by key. Change three lines in your code to this:

    $sql = "SELECT kategorie_idkategorie FROM artikel WHERE idartikel=55065";
    $query = mysqli_query($db_link, $sql);
    $row = mysqli_fetch_assoc($query);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法